from datetime import date, datetime

from pydantic import BaseModel, EmailStr, Field

from app.domain.enums import (
    CompanyBusinessType,
    CompanyCountry,
    CompanyIndustryType,
    CompanyTimeZone,
    DateDisplayFormat,
    FiscalYearStart,
    LandingPage,
    SystemCurrency,
    SystemLanguage,
    TimeDisplayFormat,
)


class OptionItem(BaseModel):
    key: str
    label: str


class GeneralSettingOptionsResponse(BaseModel):
    date_formats: list[OptionItem]
    time_formats: list[OptionItem]
    currencies: list[OptionItem]
    items_per_page: list[int]
    landing_pages: list[OptionItem]
    languages: list[OptionItem]


class PublicBrandingResponse(BaseModel):
    site_name: str
    site_tagline: str | None = None
    detail_description: str | None = None
    logo_path: str | None = None


class GeneralSettingUpdate(BaseModel):
    site_name: str | None = Field(None, min_length=1, max_length=200)
    site_tagline: str | None = Field(None, max_length=255)
    detail_description: str | None = Field(None, max_length=20000)
    date_format: DateDisplayFormat | None = None
    time_format: TimeDisplayFormat | None = None
    currency: SystemCurrency | None = None
    items_per_page: int | None = Field(None, ge=5, le=100)
    default_landing_page: LandingPage | None = None
    system_language: SystemLanguage | None = None
    enable_multi_warehouse: bool | None = None
    enable_barcode_scanning: bool | None = None
    enable_stock_alert: bool | None = None
    enable_batch_expiry: bool | None = None
    allow_negative_stock: bool | None = None
    show_product_images: bool | None = None


class GeneralSettingResponse(BaseModel):
    id: str
    company_id: str
    site_name: str
    site_tagline: str | None = None
    detail_description: str | None = None
    logo_path: str | None = None
    logo_filename: str | None = None
    date_format: DateDisplayFormat
    time_format: TimeDisplayFormat
    currency: SystemCurrency
    items_per_page: int
    default_landing_page: LandingPage
    system_language: SystemLanguage
    enable_multi_warehouse: bool
    enable_barcode_scanning: bool
    enable_stock_alert: bool
    enable_batch_expiry: bool
    allow_negative_stock: bool
    show_product_images: bool
    created_at: datetime
    updated_at: datetime

    model_config = {"from_attributes": True}


class CompanyProfileOptionsResponse(BaseModel):
    business_types: list[OptionItem]
    industry_types: list[OptionItem]
    currencies: list[OptionItem]
    fiscal_year_starts: list[OptionItem]
    time_zones: list[OptionItem]
    languages: list[OptionItem]
    countries: list[OptionItem]
    states: list[str]


class CompanyProfileUpdate(BaseModel):
    company_name: str | None = Field(None, min_length=1, max_length=200)
    date_of_establishment: date | None = None
    company_tagline: str | None = Field(None, max_length=255)
    business_type: CompanyBusinessType | None = None
    industry_type: CompanyIndustryType | None = None
    currency: SystemCurrency | None = None
    registration_number: str | None = Field(None, max_length=100)
    fiscal_year_start: FiscalYearStart | None = None
    tax_number: str | None = Field(None, max_length=100)
    time_zone: CompanyTimeZone | None = None
    website: str | None = Field(None, max_length=255)
    default_language: SystemLanguage | None = None
    address: str | None = Field(None, min_length=1)
    country: CompanyCountry | None = None
    state_province: str | None = Field(None, min_length=1, max_length=100)
    city: str | None = Field(None, min_length=1, max_length=100)
    postal_code: str | None = Field(None, min_length=1, max_length=20)
    phone: str | None = Field(None, max_length=50)
    alternate_phone: str | None = Field(None, max_length=50)
    email: EmailStr | None = None
    alternate_email: EmailStr | None = None
    fax: str | None = Field(None, max_length=50)
    number_of_employees: str | None = Field(None, max_length=50)
    description: str | None = None
    notes: str | None = None
    facebook_url: str | None = Field(None, max_length=255)
    twitter_url: str | None = Field(None, max_length=255)
    linkedin_url: str | None = Field(None, max_length=255)
    instagram_url: str | None = Field(None, max_length=255)
    youtube_url: str | None = Field(None, max_length=255)
    whatsapp: str | None = Field(None, max_length=50)


class CompanyProfileResponse(BaseModel):
    id: str
    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
    registration_number: str | None = None
    fiscal_year_start: FiscalYearStart
    tax_number: str | None = None
    time_zone: CompanyTimeZone
    website: str | None = None
    default_language: SystemLanguage
    logo_path: str | None = None
    logo_filename: str | None = None
    address: str
    country: CompanyCountry
    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
    updated_at: datetime

    model_config = {"from_attributes": True}
