# ============================================================
# ADDITIVE: Hiring Plus - maker/checker, identity (Aadhaar / PAN)
# verification, hike policy, hiring kit, upload links, role map.
#
# Nothing existing is removed. All tables below are NEW.
# Imported at the bottom of Recruitment/models.py.
# ============================================================
from django.db import models

from Company.models import company_details
from Employee.models import EmployeeDT

from .models import AuditMixin, JobRequisition


# ------------------------------------------------------------
# 1. MAKER / CHECKER on candidate creation
# ------------------------------------------------------------
class CandidateApproval(AuditMixin):
    STATUS = [("Pending", "Pending"), ("Approved", "Approved"), ("Rejected", "Rejected")]

    company = models.ForeignKey(company_details, on_delete=models.CASCADE, related_name="candidate_approvals")
    candidate = models.OneToOneField(EmployeeDT, on_delete=models.CASCADE, related_name="creation_approval")
    status = models.CharField(max_length=10, choices=STATUS, default="Pending", db_index=True)
    maker = models.CharField(max_length=150, blank=True, null=True)      # actor label of the creator
    checker = models.CharField(max_length=150, blank=True, null=True)    # actor label of the M-level approver
    remarks = models.TextField(blank=True, null=True)
    decided_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        ordering = ["-created_at"]


# ------------------------------------------------------------
# 2. HIKE POLICY approval (last CTC vs offered CTC)
# ------------------------------------------------------------
class HikeApproval(AuditMixin):
    STATUS = [("Pending", "Pending"), ("Approved", "Approved"), ("Rejected", "Rejected")]

    company = models.ForeignKey(company_details, on_delete=models.CASCADE, related_name="hike_approvals")
    candidate = models.ForeignKey(EmployeeDT, on_delete=models.CASCADE, related_name="hike_approvals")
    offer = models.ForeignKey("Recruitment.Offer", null=True, blank=True, on_delete=models.SET_NULL,
                              related_name="hike_approvals")
    last_ctc_lpa = models.DecimalField(max_digits=8, decimal_places=2)
    offered_ctc_lpa = models.DecimalField(max_digits=8, decimal_places=2)
    hike_percent = models.DecimalField(max_digits=7, decimal_places=2)
    limit_percent = models.DecimalField(max_digits=5, decimal_places=2)
    status = models.CharField(max_length=10, choices=STATUS, default="Pending", db_index=True)
    requested_by = models.CharField(max_length=150, blank=True, null=True)
    decided_by = models.CharField(max_length=150, blank=True, null=True)
    decided_at = models.DateTimeField(null=True, blank=True)
    remarks = models.TextField(blank=True, null=True)

    class Meta:
        ordering = ["-created_at"]


# ------------------------------------------------------------
# 3. IDENTITY VERIFICATION (PAN / Aadhaar / Education / Experience)
#    Full Aadhaar number is NEVER stored here: only last-4 + HMAC hash.
# ------------------------------------------------------------
class IdentityVerification(AuditMixin):
    TYPES = [("AADHAAR", "Aadhaar"), ("PAN", "PAN"), ("EDUCATION", "Education Certificate"),
             ("EXPERIENCE", "Experience")]
    STATUS = [("Pending", "Pending"), ("OTP Sent", "OTP Sent"), ("Verified", "Verified"),
              ("Mismatch", "Mismatch"), ("Failed", "Failed"), ("Not Applicable", "Not Applicable")]
    METHOD = [("None", "None"), ("Checksum", "Checksum"), ("OTP", "Aadhaar OTP"), ("Manual", "Manual / Document"),
              ("Provider", "Provider API")]

    company = models.ForeignKey(company_details, on_delete=models.CASCADE, related_name="identity_verifications")
    candidate = models.ForeignKey(EmployeeDT, on_delete=models.CASCADE, related_name="identity_verifications")
    id_type = models.CharField(max_length=12, choices=TYPES, db_index=True)
    status = models.CharField(max_length=15, choices=STATUS, default="Pending", db_index=True)
    method = models.CharField(max_length=10, choices=METHOD, default="None")
    id_last4 = models.CharField(max_length=4, blank=True, null=True)
    id_hash = models.CharField(max_length=64, blank=True, null=True, db_index=True)
    provider = models.CharField(max_length=40, blank=True, null=True)
    provider_ref = models.CharField(max_length=120, blank=True, null=True)
    name_on_record = models.CharField(max_length=200, blank=True, null=True)
    name_match = models.BooleanField(null=True, blank=True)
    dob_match = models.BooleanField(null=True, blank=True)
    remarks = models.TextField(blank=True, null=True)
    verified_by = models.CharField(max_length=150, blank=True, null=True)
    verified_at = models.DateTimeField(null=True, blank=True)
    meta = models.JSONField(default=dict, blank=True)

    class Meta:
        unique_together = (("candidate", "id_type"),)
        ordering = ["id"]


# ------------------------------------------------------------
# 4. HIRING KIT: to-do checklist, hiring questions, interviewer panel
# ------------------------------------------------------------
class HiringChecklistItem(AuditMixin):
    CATEGORY = [("Todo", "To-do"), ("Documentation", "Documentation")]

    company = models.ForeignKey(company_details, on_delete=models.CASCADE, related_name="hiring_checklist")
    candidate = models.ForeignKey(EmployeeDT, on_delete=models.CASCADE, related_name="hiring_checklist")
    title = models.CharField(max_length=200)
    category = models.CharField(max_length=14, choices=CATEGORY, default="Todo")
    doc_type = models.CharField(max_length=30, blank=True, null=True)   # EmployeeDocument.document_type it maps to
    is_done = models.BooleanField(default=False)
    done_by = models.CharField(max_length=150, blank=True, null=True)
    done_at = models.DateTimeField(null=True, blank=True)
    due_date = models.DateField(null=True, blank=True)
    sort_order = models.PositiveIntegerField(default=0)

    class Meta:
        ordering = ["sort_order", "id"]


class HiringQuestion(AuditMixin):
    company = models.ForeignKey(company_details, on_delete=models.CASCADE, related_name="hiring_questions")
    requisition = models.ForeignKey(JobRequisition, null=True, blank=True, on_delete=models.CASCADE,
                                    related_name="hiring_questions")
    candidate = models.ForeignKey(EmployeeDT, null=True, blank=True, on_delete=models.CASCADE,
                                  related_name="hiring_questions")
    question = models.TextField()
    category = models.CharField(max_length=60, blank=True, null=True)   # Technical / Culture / Compensation ...
    answer = models.TextField(blank=True, null=True)
    asked_by = models.CharField(max_length=150, blank=True, null=True)
    sort_order = models.PositiveIntegerField(default=0)

    class Meta:
        ordering = ["sort_order", "id"]


class InterviewerAssignment(AuditMixin):
    company = models.ForeignKey(company_details, on_delete=models.CASCADE, related_name="interviewer_assignments")
    candidate = models.ForeignKey(EmployeeDT, on_delete=models.CASCADE, related_name="interviewer_panel")
    interviewer = models.ForeignKey(EmployeeDT, on_delete=models.CASCADE, related_name="panel_assignments")
    round_name = models.CharField(max_length=100, blank=True, null=True)
    is_confirmed = models.BooleanField(default=False)

    class Meta:
        unique_together = (("candidate", "interviewer", "round_name"),)
        ordering = ["id"]


# ------------------------------------------------------------
# 5. SHAREABLE UPLOAD LINK (candidate uploads documents, no login)
# ------------------------------------------------------------
class DocumentUploadLink(AuditMixin):
    company = models.ForeignKey(company_details, on_delete=models.CASCADE, related_name="doc_upload_links")
    candidate = models.ForeignKey(EmployeeDT, on_delete=models.CASCADE, related_name="doc_upload_links")
    token = models.CharField(max_length=80, unique=True)
    document_types = models.JSONField(default=list, blank=True)
    expires_at = models.DateTimeField()
    max_uses = models.PositiveIntegerField(default=20)
    use_count = models.PositiveIntegerField(default=0)
    revoked = models.BooleanField(default=False)
    sent_to = models.CharField(max_length=254, blank=True, null=True)
    last_used_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        ordering = ["-created_at"]


# ------------------------------------------------------------
# 6. POST-HIRE ROLE MAP on the requisition (HCM / reporting mgr / reviewer)
# ------------------------------------------------------------
class RequisitionRoles(AuditMixin):
    company = models.ForeignKey(company_details, on_delete=models.CASCADE, related_name="requisition_roles")
    requisition = models.OneToOneField(JobRequisition, on_delete=models.CASCADE, related_name="role_map")
    hcm = models.ForeignKey(EmployeeDT, null=True, blank=True, on_delete=models.SET_NULL, related_name="+")
    reporting_manager = models.ForeignKey(EmployeeDT, null=True, blank=True, on_delete=models.SET_NULL,
                                          related_name="+")
    performance_reviewer = models.ForeignKey(EmployeeDT, null=True, blank=True, on_delete=models.SET_NULL,
                                             related_name="+")
