from datetime import date, datetime
from pathlib import Path

from pydantic import BaseModel, Field

from app.domain.enums import (
    CompanyBusinessType,
    CompanyCountry,
    CompanyIndustryType,
    CompanyTimeZone,
    DateDisplayFormat,
    FiscalYearStart,
    LandingPage,
    SystemCurrency,
    SystemLanguage,
    TimeDisplayFormat,
)


class GeneralSetting(BaseModel):
    id: str | None = None
    company_id: str
    site_name: str = "Inventory System"
    site_tagline: str | None = "Manage your inventory efficiently"
    detail_description: str | None = None
    logo_path: str | None = None
    date_format: DateDisplayFormat = DateDisplayFormat.DD_MM_YYYY
    time_format: TimeDisplayFormat = TimeDisplayFormat.HOUR_12
    currency: SystemCurrency = SystemCurrency.PKR
    items_per_page: int = 10
    default_landing_page: LandingPage = LandingPage.DASHBOARD
    system_language: SystemLanguage = SystemLanguage.ENGLISH
    enable_multi_warehouse: bool = True
    enable_barcode_scanning: bool = True
    enable_stock_alert: bool = True
    enable_batch_expiry: bool = True
    allow_negative_stock: bool = False
    show_product_images: bool = True
    created_at: datetime = Field(default_factory=datetime.utcnow)
    updated_at: datetime = Field(default_factory=datetime.utcnow)

    model_config = {"from_attributes": True}

    @property
    def logo_filename(self) -> str | None:
        if not self.logo_path:
            return None
        return Path(self.logo_path).name


class CompanyProfile(BaseModel):
    id: str | None = None
    company_id: str
    company_name: str
    date_of_establishment: date | None = None
    company_tagline: str | None = None
    business_type: CompanyBusinessType | None = None
    industry_type: CompanyIndustryType | None = None
    currency: SystemCurrency = SystemCurrency.PKR
    registration_number: str | None = None
    fiscal_year_start: FiscalYearStart = FiscalYearStart.JULY
    tax_number: str | None = None
    time_zone: CompanyTimeZone = CompanyTimeZone.ASIA_KARACHI
    website: str | None = None
    default_language: SystemLanguage = SystemLanguage.ENGLISH
    logo_path: str | None = None
    address: str
    country: CompanyCountry = CompanyCountry.PAKISTAN
    state_province: str = ""
    city: str = ""
    postal_code: str = ""
    phone: str | None = None
    alternate_phone: str | None = None
    email: str | None = None
    alternate_email: str | None = None
    fax: str | None = None
    number_of_employees: str | None = None
    description: str | None = None
    notes: str | None = None
    facebook_url: str | None = None
    twitter_url: str | None = None
    linkedin_url: str | None = None
    instagram_url: str | None = None
    youtube_url: str | None = None
    whatsapp: str | None = None
    created_at: datetime = Field(default_factory=datetime.utcnow)
    updated_at: datetime = Field(default_factory=datetime.utcnow)

    model_config = {"from_attributes": True}

    @property
    def logo_filename(self) -> str | None:
        if not self.logo_path:
            return None
        return Path(self.logo_path).name

