import uuid
from sqlalchemy import Column, String, Boolean, Text, Integer, Numeric, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from app.models.base import BaseModel


class Company(BaseModel):
    """
    Top-level tenant entity.  Each company owns its own set of branches,
    users, and all transactional data.  No data crosses company boundaries.
    """
    __tablename__ = "companies"

    id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))

    # ── Identity ────────────────────────────────────────────────────────────
    name = Column(String(255), nullable=False)          # Trading / display name
    legal_name = Column(String(255))                    # Full legal entity name
    company_code = Column(String(20), unique=True)      # Short code used at login
    registration_number = Column(String(100))           # Business/company reg #
    tax_identification_number = Column(String(100))     # TIN / PAYE number
    vat_registration_number = Column(String(100))       # VAT registration number
    industry = Column(String(100))                      # e.g. Retail, Manufacturing

    # ── Contact ─────────────────────────────────────────────────────────────
    address_line1 = Column(String(255))
    address_line2 = Column(String(255))
    city = Column(String(100))
    state = Column(String(100))
    country = Column(String(100), default="Botswana")
    postal_code = Column(String(20))
    phone = Column(String(50))
    phone2 = Column(String(50))
    fax = Column(String(50))
    email = Column(String(255))
    website = Column(String(255))

    # ── Branding ────────────────────────────────────────────────────────────
    logo_url = Column(String(500))
    primary_color = Column(String(10), default="#0d6efd")   # hex colour for docs

    # ── Financial defaults ───────────────────────────────────────────────────
    currency = Column(String(10), default="BWP")
    default_vat_rate = Column(Numeric(5, 2), default=14.0)
    fiscal_year_start_month = Column(Integer, default=1)    # 1 = January
    timezone = Column(String(50), default="Africa/Gaborone")

    # ── Document / invoice footer text ──────────────────────────────────────
    invoice_footer = Column(Text)
    receipt_footer = Column(Text)
    po_terms = Column(Text)                                 # Default PO terms

    # ── Lifecycle ───────────────────────────────────────────────────────────
    is_active = Column(Boolean, default=True)
    is_setup_complete = Column(Boolean, default=False)
    setup_completed_at = Column(DateTime)
    subscription_plan = Column(String(50), default="standard")

    # ── Licensing (CNP Solutions purchase token) ─────────────────────────────
    license_key_id = Column(String, ForeignKey("license_keys.id"), nullable=True, index=True)
    license_expires_at = Column(DateTime, nullable=True)
    deployment_mode = Column(String(20), default="cloud")  # cloud | local | hybrid

    # ── Relationships ────────────────────────────────────────────────────────
    branches = relationship("Branch", back_populates="company", lazy="select")
    users = relationship("User", back_populates="company", lazy="select")
    license_key = relationship(
        "LicenseKey",
        foreign_keys=[license_key_id],
        primaryjoin="Company.license_key_id==LicenseKey.id",
        uselist=False,
    )
    deployment_nodes = relationship("DeploymentNode", back_populates="company", lazy="select")
    branch_licenses = relationship("BranchLicense", back_populates="company", lazy="select")
