"""Foundation DTOs."""
from __future__ import annotations

from datetime import date, datetime
from uuid import UUID

from pydantic import BaseModel, Field

from app.models.types import AccountCategory, AccountStatus, AccountType, FiscalPeriodStatus
from app.schemas.common import ORMBase


class ChartOfAccountsCreate(BaseModel):
    name: str = Field(max_length=255)
    code: str = Field(max_length=50)
    description: str | None = None
    is_default: bool = False


class ChartOfAccountsUpdate(BaseModel):
    name: str | None = Field(default=None, max_length=255)
    description: str | None = None
    is_default: bool | None = None
    is_active: bool | None = None


class ChartOfAccountsRead(ORMBase):
    id: UUID
    tenant_id: UUID
    name: str
    code: str
    description: str | None
    is_default: bool
    is_active: bool
    created_at: datetime


class AccountCreate(BaseModel):
    chart_id: UUID
    parent_id: UUID | None = None
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    account_type: AccountType
    account_category: AccountCategory
    level: int = 1
    is_group: bool = False
    is_postable: bool = True
    currency_id: UUID | None = None
    description: str | None = None


class AccountUpdate(BaseModel):
    parent_id: UUID | None = None
    name: str | None = Field(default=None, max_length=255)
    account_type: AccountType | None = None
    account_category: AccountCategory | None = None
    level: int | None = None
    is_group: bool | None = None
    is_postable: bool | None = None
    currency_id: UUID | None = None
    description: str | None = None
    status: AccountStatus | None = None


class AccountRead(ORMBase):
    id: UUID
    tenant_id: UUID
    chart_id: UUID
    parent_id: UUID | None = None
    code: str
    name: str
    account_type: AccountType
    account_category: AccountCategory
    level: int
    is_group: bool
    is_postable: bool
    status: AccountStatus
    description: str | None = None
    created_at: datetime


class CurrencyCreate(BaseModel):
    code: str = Field(max_length=3)
    name: str = Field(max_length=100)
    symbol: str | None = None
    decimal_places: int = 2
    is_base: bool = False


class CurrencyUpdate(BaseModel):
    name: str | None = Field(default=None, max_length=100)
    symbol: str | None = None
    decimal_places: int | None = None
    is_base: bool | None = None
    is_active: bool | None = None


class CurrencyRead(ORMBase):
    id: UUID
    tenant_id: UUID
    code: str
    name: str
    symbol: str | None
    is_base: bool
    is_active: bool


class FiscalYearCreate(BaseModel):
    name: str
    start_date: date
    end_date: date
    is_current: bool = False


class FiscalYearUpdate(BaseModel):
    name: str | None = None
    start_date: date | None = None
    end_date: date | None = None
    is_current: bool | None = None


class FiscalYearRead(ORMBase):
    id: UUID
    tenant_id: UUID
    name: str
    start_date: date
    end_date: date
    is_current: bool
    is_closed: bool


class FiscalPeriodCreate(BaseModel):
    fiscal_year_id: UUID
    name: str
    period_number: int
    start_date: date
    end_date: date
    is_current: bool = False


class FiscalPeriodUpdate(BaseModel):
    name: str | None = None
    start_date: date | None = None
    end_date: date | None = None
    is_current: bool | None = None
    status: FiscalPeriodStatus | None = None


class FiscalPeriodRead(ORMBase):
    id: UUID
    tenant_id: UUID
    fiscal_year_id: UUID
    name: str
    period_number: int
    start_date: date
    end_date: date
    status: FiscalPeriodStatus
    is_current: bool


class CostCenterCreate(BaseModel):
    code: str
    name: str
    parent_id: UUID | None = None


class CostCenterUpdate(BaseModel):
    name: str | None = None
    parent_id: UUID | None = None
    is_active: bool | None = None


class CostCenterRead(ORMBase):
    id: UUID
    tenant_id: UUID
    code: str
    name: str
    parent_id: UUID | None = None
    is_active: bool


class ProjectCreate(BaseModel):
    code: str
    name: str
    start_date: date | None = None
    end_date: date | None = None


class ProjectUpdate(BaseModel):
    name: str | None = None
    start_date: date | None = None
    end_date: date | None = None
    is_active: bool | None = None


class ProjectRead(ORMBase):
    id: UUID
    tenant_id: UUID
    code: str
    name: str
    start_date: date | None = None
    end_date: date | None = None
    is_active: bool


class SetupStatusRead(BaseModel):
    has_chart: bool
    has_accounts: bool
    has_currency: bool
    has_base_currency: bool
    has_fiscal_year: bool
    has_fiscal_period: bool
    has_cash_box: bool
    has_customer: bool
    has_voucher: bool
    completed_steps: int
    total_steps: int
    percent: int


class CoaTemplateAccount(BaseModel):
    code: str
    name: str
    account_type: AccountType
    account_category: AccountCategory
    level: int = 1
    is_group: bool = False
    is_postable: bool = True
    parent_code: str | None = None


class CoaTemplateRead(BaseModel):
    id: str
    name: str
    description: str
    business_type: str
    accounts: list[CoaTemplateAccount]


class CoaTemplateImportRequest(BaseModel):
    chart_id: UUID | None = None
    chart_name: str | None = None
    chart_code: str | None = None
