from __future__ import annotations

from datetime import date, datetime, timezone

from sqlalchemy import Boolean, Date, DateTime, ForeignKey, Integer, JSON, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column

from app.core.db.common import CommonBase


class Client(CommonBase):
    __tablename__ = "clients"
    __table_args__ = (
        UniqueConstraint("tenant_id", "client_code", name="uq_clients_tenant_code"),
        UniqueConstraint("tenant_id", "pan", name="uq_clients_tenant_pan"),
        UniqueConstraint("tenant_id", "gstin", name="uq_clients_tenant_gstin"),
    )

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    tenant_id: Mapped[int] = mapped_column(ForeignKey("tenants.id"), nullable=False, index=True)
    branch_id: Mapped[int] = mapped_column(ForeignKey("branches.id"), nullable=False, index=True)
    partner_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"), nullable=True, index=True)
    default_review_partner_user_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"), nullable=True, index=True)
    engagement_mode: Mapped[str] = mapped_column(String(30), nullable=False, default="internal_managed", index=True)
    client_code: Mapped[str] = mapped_column(String(50), nullable=False, index=True)
    client_name: Mapped[str] = mapped_column(String(200), nullable=False, index=True)
    trade_name: Mapped[str | None] = mapped_column(String(200), nullable=True)
    client_type: Mapped[str] = mapped_column(String(100), nullable=False, default="Other")
    pan: Mapped[str | None] = mapped_column(String(20), nullable=True, index=True)
    gstin: Mapped[str | None] = mapped_column(String(20), nullable=True, index=True)
    tan: Mapped[str | None] = mapped_column(String(20), nullable=True)
    cin_llpin: Mapped[str | None] = mapped_column(String(30), nullable=True)
    msme_no: Mapped[str | None] = mapped_column(String(50), nullable=True)
    iec_code: Mapped[str | None] = mapped_column(String(30), nullable=True)
    contact_person_name: Mapped[str | None] = mapped_column(String(200), nullable=True)
    contact_person_designation: Mapped[str | None] = mapped_column(String(200), nullable=True)
    mobile: Mapped[str | None] = mapped_column(String(20), nullable=True)
    alternate_mobile: Mapped[str | None] = mapped_column(String(20), nullable=True)
    email: Mapped[str | None] = mapped_column(String(255), nullable=True)
    alternate_email: Mapped[str | None] = mapped_column(String(255), nullable=True)
    address_line_1: Mapped[str | None] = mapped_column(String(255), nullable=True)
    address_line_2: Mapped[str | None] = mapped_column(String(255), nullable=True)
    city: Mapped[str | None] = mapped_column(String(100), nullable=True)
    state: Mapped[str | None] = mapped_column(String(100), nullable=True)
    pincode: Mapped[str | None] = mapped_column(String(20), nullable=True)
    country: Mapped[str | None] = mapped_column(String(100), nullable=True, default="India")
    status: Mapped[str] = mapped_column(String(20), nullable=False, default="active", index=True)
    client_category: Mapped[str | None] = mapped_column(String(100), nullable=True)
    risk_category: Mapped[str | None] = mapped_column(String(50), nullable=True)
    onboarding_date: Mapped[date | None] = mapped_column(Date, nullable=True)
    closing_date: Mapped[date | None] = mapped_column(Date, nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    gst_applicable: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    income_tax_applicable: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    tds_applicable: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    roc_applicable: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    audit_applicable: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    pf_applicable: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    esi_applicable: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    professional_tax_applicable: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    payroll_applicable: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    msme_applicable: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    import_export_applicable: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
    is_archived: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    archived_at_utc: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
    created_by_user_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"), nullable=True)
    updated_by_user_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"), nullable=True)
    portal_user_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"), nullable=True, index=True)
    created_at_utc: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), nullable=False)
    updated_at_utc: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc), nullable=False)


class ClientAuditLog(CommonBase):
    __tablename__ = "client_audit_logs"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    client_id: Mapped[int] = mapped_column(ForeignKey("clients.id"), nullable=False, index=True)
    tenant_id: Mapped[int] = mapped_column(ForeignKey("tenants.id"), nullable=False, index=True)
    branch_id: Mapped[int] = mapped_column(ForeignKey("branches.id"), nullable=False, index=True)
    actor_user_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"), nullable=True)
    action: Mapped[str] = mapped_column(String(50), nullable=False, index=True)
    summary: Mapped[str] = mapped_column(String(255), nullable=False)
    payload_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)
    created_at_utc: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), nullable=False)
