from django.db import models


class PersonnelProfile(models.Model):
    """HR data of one member. Created lazily on first edit; the organization is copied from the
    membership and checked on every query."""

    EMPLOYMENT_TYPES = [("full_time", "full_time"), ("part_time", "part_time"), ("contract", "contract"), ("intern", "intern")]

    organization = models.ForeignKey("organizations.Organization", on_delete=models.CASCADE, related_name="personnel_profiles")
    membership = models.OneToOneField("organizations.OrganizationMembership", on_delete=models.CASCADE, related_name="personnel_profile")
    job_title = models.CharField(max_length=120, blank=True, default="")
    department = models.CharField(max_length=120, blank=True, default="")
    employment_type = models.CharField(max_length=20, choices=EMPLOYMENT_TYPES, default="full_time")
    hire_date = models.DateField(null=True, blank=True)
    end_date = models.DateField(null=True, blank=True)
    phone = models.CharField(max_length=30, blank=True, default="")
    emergency_contact = models.CharField(max_length=200, blank=True, default="")
    notes = models.TextField(blank=True, default="")
    updated_at = models.DateTimeField(auto_now=True)


class Contract(models.Model):
    STATUSES = [("active", "active"), ("ended", "ended")]
    PAY_TYPES = [("monthly", "monthly"), ("daily", "daily"), ("hourly", "hourly")]

    organization = models.ForeignKey("organizations.Organization", on_delete=models.CASCADE, related_name="contracts")
    membership = models.ForeignKey("organizations.OrganizationMembership", on_delete=models.CASCADE, related_name="contracts")
    title = models.CharField(max_length=150)
    start_date = models.DateField()
    end_date = models.DateField(null=True, blank=True)
    pay_type = models.CharField(max_length=10, choices=PAY_TYPES, default="monthly")
    pay_amount = models.BigIntegerField(null=True, blank=True)  # per month / per day / per hour, by pay_type
    status = models.CharField(max_length=10, choices=STATUSES, default="active")
    notes = models.TextField(blank=True, default="")
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["-start_date", "-id"]
