# ============================================================
# ADDITIVE: Letterhead settings of a company (Document & Letter Issuance Center).
# Imported at the bottom of People/models.py.
#
# Nothing here duplicates the company master: every blank field falls back to the Company model
# (Registered_Company_Name, Company_logo, Website, REGISTERED_OFFICE_add ...) at generation time.
# Only what the company master does not have is stored here: CIN, stamp, signature, signatory, place, policy defaults.
# ============================================================
from django.db import models

from Company.models import company_details


def upload_logo(instance, filename):
    return f"letterheads/{instance.company_id}/logo_{filename}"


def upload_stamp(instance, filename):
    return f"letterheads/{instance.company_id}/stamp_{filename}"


def upload_signature(instance, filename):
    return f"letterheads/{instance.company_id}/signature_{filename}"


def default_policy():
    return {"probation_months": 3, "notice_probation_days": 7, "notice_confirmed_days": 30,
            "leave_in_probation_days": 2, "salary_disbursal": "1st to 10th of every month",
            "employment_status": "Full-time"}


class CompanyLetterhead(models.Model):
    HEADER = [("FULL", "Logo + company block + line"), ("LOGO_ONLY", "Logo only"), ("NONE", "No header")]
    PAGE = [("A4", "A4"), ("LETTER", "US Letter")]

    company = models.OneToOneField(company_details, on_delete=models.CASCADE, related_name="letterhead")
    legal_name = models.CharField(max_length=200, blank=True, default="")          # header block, e.g. GLAZE ROCKS PVT LTD
    footer_name = models.CharField(max_length=200, blank=True, default="")         # footer, e.g. GLAZE ROCKS PRIVATE LIMITED
    short_name = models.CharField(max_length=120, blank=True, default="")          # "For Glaze Rocks Pvt. Ltd."
    cin = models.CharField(max_length=25, blank=True, default="", db_index=True)
    website = models.CharField(max_length=200, blank=True, default="")
    registered_office = models.TextField(blank=True, default="")
    footer_address = models.TextField(blank=True, default="")
    place = models.CharField(max_length=80, blank=True, default="")
    signatory_name = models.CharField(max_length=150, blank=True, default="")
    signatory_title = models.CharField(max_length=100, blank=True, default="Authorized Signatory")
    logo = models.FileField(upload_to=upload_logo, null=True, blank=True)
    stamp = models.FileField(upload_to=upload_stamp, null=True, blank=True)
    signature = models.FileField(upload_to=upload_signature, null=True, blank=True)
    header_style = models.CharField(max_length=10, choices=HEADER, default="FULL")
    footer_enabled = models.BooleanField(default=True)
    page_size = models.CharField(max_length=6, choices=PAGE, default="A4")
    body_font_size = models.PositiveSmallIntegerField(default=11)
    show_ref_no = models.BooleanField(default=False)
    policy_defaults = models.JSONField(default=default_policy, blank=True)         # used by offer / appointment text
    created_by = models.CharField(max_length=150, null=True, blank=True)
    updated_by = models.CharField(max_length=150, null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
