from pydantic import BaseModel
from typing import Optional, List
from datetime import datetime
from decimal import Decimal
from uuid import UUID
from .base_responses import BaseResponse, CrudCollection


class AccountBooksResponse(BaseResponse):
    """Response model for account books"""

    country: str
    branch_type: int
    company_id: int
    company_uu_id: str
    branch_id: Optional[int]
    branch_uu_id: Optional[str]


class AccountBooksCollection(CrudCollection[AccountBooksResponse]):
    """Collection of account books"""

    pass


class AccountCodesResponse(BaseResponse):
    """Response model for account codes"""

    account_code: str
    comment_line: str
    is_receive_or_debit: bool
    product_id: int = 0
    nvi_id: str = ""
    status_id: int = 0
    account_code_seperator: str = "."
    system_id: int = 0
    locked: bool = False
    company_id: Optional[int]
    company_uu_id: str
    customer_id: Optional[int]
    customer_uu_id: str
    person_id: Optional[int]
    person_uu_id: str


class AccountCodesCollection(CrudCollection[AccountCodesResponse]):
    """Collection of account codes"""

    pass


class AccountCodeParserResponse(BaseResponse):
    """Response model for account code parser"""

    account_code_1: str
    account_code_2: str
    account_code_3: str
    account_code_4: str = ""
    account_code_5: str = ""
    account_code_6: str = ""
    account_code_id: int
    account_code_uu_id: str


class AccountCodeParserCollection(CrudCollection[AccountCodeParserResponse]):
    """Collection of account code parsers"""

    pass


class AccountMasterResponse(BaseResponse):
    """Response model for account master"""

    doc_date: datetime
    plug_type: str
    plug_number: int
    special_code: str = ""
    authorization_code: str = ""
    doc_code: str = ""
    doc_type: int = 0
    comment_line1: str = ""
    comment_line2: str = ""
    comment_line3: str = ""
    comment_line4: str = ""
    comment_line5: str = ""
    comment_line6: str = ""
    project_code: str = ""
    module_no: str = ""
    journal_no: int = 0
    status_id: int = 0
    canceled: bool = False
    print_count: int = 0
    total_active: Decimal = Decimal("0")
    total_passive: Decimal = Decimal("0")
    total_active_1: Decimal = Decimal("0")
    total_passive_1: Decimal = Decimal("0")
    total_active_2: Decimal = Decimal("0")
    total_passive_2: Decimal = Decimal("0")
    total_active_3: Decimal = Decimal("0")
    total_passive_3: Decimal = Decimal("0")
    total_active_4: Decimal = Decimal("0")
    total_passive_4: Decimal = Decimal("0")
    cross_ref: int = 0
    data_center_id: str = ""
    data_center_rec_num: int = 0
    account_header_id: int
    account_header_uu_id: str
    project_item_id: Optional[int]
    project_item_uu_id: Optional[str]
    department_id: Optional[int]
    department_uu_id: Optional[str]


class AccountMasterCollection(CrudCollection[AccountMasterResponse]):
    """Collection of account masters"""

    pass


class AccountDetailResponse(BaseResponse):
    """Response model for account detail"""

    doc_date: datetime
    line_no: int
    receive_debit: str
    debit: Decimal
    department: str = ""
    special_code: str = ""
    account_ref: int = 0
    account_fiche_ref: int = 0
    center_ref: int = 0
    general_code: str = ""
    credit: Decimal = Decimal("0")
    currency_type: str = "TL"
    exchange_rate: Decimal = Decimal("0")
    debit_cur: Decimal = Decimal("0")
    credit_cur: Decimal = Decimal("0")
    discount_cur: Decimal = Decimal("0")
    amount: Decimal = Decimal("0")
    cross_account_code: str = ""
    inf_index: Decimal = Decimal("0")
    not_inflated: int = 0
    not_calculated: int = 0
    comment_line1: str = ""
    comment_line2: str = ""
    comment_line3: str = ""
    comment_line4: str = ""
    comment_line5: str = ""
    comment_line6: str = ""
    owner_acc_ref: int = 0
    from_where: int = 0
    orj_eid: int = 0
    canceled: int = 0
    cross_ref: int = 0
    data_center_id: str = ""
    data_center_rec_num: str = "0"
    status_id: int = 0
    plug_type_id: Optional[int]
    plug_type_uu_id: str
    account_header_id: int
    account_header_uu_id: str
    account_code_id: int
    account_code_uu_id: str
    account_master_id: int
    account_master_uu_id: str
    project_id: Optional[int]
    project_uu_id: Optional[str]


class AccountDetailCollection(CrudCollection[AccountDetailResponse]):
    """Collection of account details"""

    pass


class AccountRecordResponse(BaseResponse):
    """Response model for account financial records.

    This model represents a financial transaction record in the system,
    including bank transaction details, amounts, and related metadata.

    Attributes:
        iban (str): International Bank Account Number
        bank_date (datetime): Date when the transaction occurred at the bank
        currency_value (Decimal): Original transaction amount
        bank_balance (Decimal): Account balance after the transaction
        currency (str): Currency code (e.g., "TRY", "USD")
        additional_balance (Decimal): Any additional balance adjustments
        channel_branch (str): Bank branch or channel where transaction occurred
        process_name (str): Name/type of the transaction
        process_type (str): Classification of the transaction
        process_comment (str): Additional transaction details or notes
        bank_reference_code (str): Bank's reference code for the transaction
        add_comment_note (Optional[str]): Additional internal notes
        is_receipt_mail_send (Optional[bool]): Whether receipt was emailed
        found_from (Optional[str]): Source of the transaction record
        similarity (Optional[float]): Matching confidence for duplicate detection
        remainder_balance (Optional[Decimal]): Remaining balance if partial
        bank_date_y (Optional[int]): Year of bank transaction
        bank_date_m (Optional[int]): Month of bank transaction
        bank_date_w (Optional[int]): Week of bank transaction
        bank_date_d (Optional[int]): Day of bank transaction
        approving_accounting_record (Optional[bool]): Accounting approval status
        accounting_receipt_date (Optional[datetime]): When receipt was processed
        accounting_receipt_number (Optional[int]): Receipt reference number
        approved_record (Optional[bool]): Whether record is approved
        import_file_name (Optional[str]): Source file if imported
        receive_debit_uu_id (Optional[str]): Related debit record ID
        budget_type_uu_id (Optional[str]): Associated budget type ID
        company_uu_id (Optional[str]): Associated company ID
        send_company_uu_id (Optional[str]): Sending company ID
        customer_id (Optional[str]): Associated customer ID
        customer_uu_id (Optional[str]): Associated customer UUID
        send_person_uu_id (Optional[str]): Sending person ID
        approving_accounting_person_uu_id (Optional[str]): Approver ID
        build_parts_uu_id (Optional[str]): Related building part ID
        build_decision_book_uu_id (Optional[str]): Related decision book ID
    """

    iban: str
    bank_date: datetime
    currency_value: Decimal
    bank_balance: Decimal
    currency: str = "TRY"
    additional_balance: Decimal = Decimal("0")
    channel_branch: str
    process_name: str
    process_type: str
    process_comment: str
    bank_reference_code: str
    add_comment_note: Optional[str]
    is_receipt_mail_send: Optional[bool] = False
    found_from: Optional[str]
    similarity: Optional[float]
    remainder_balance: Optional[Decimal]
    bank_date_y: Optional[int]
    bank_date_m: Optional[int]
    bank_date_w: Optional[int]
    bank_date_d: Optional[int]
    approving_accounting_record: Optional[bool]
    accounting_receipt_date: Optional[datetime]
    accounting_receipt_number: Optional[int]
    approved_record: Optional[bool]
    import_file_name: Optional[str]
    receive_debit_uu_id: Optional[str]
    budget_type_uu_id: Optional[str]
    company_uu_id: Optional[str]
    send_company_uu_id: Optional[str]
    customer_id: Optional[str]
    customer_uu_id: Optional[str]
    send_person_uu_id: Optional[str]
    approving_accounting_person_uu_id: Optional[str]
    build_parts_uu_id: Optional[str]
    build_decision_book_uu_id: Optional[str]


class AccountRecordCollection(CrudCollection[AccountRecordResponse]):
    """Collection of account financial records.

    This model represents a paginated list of account records with
    sorting and pagination information.
    """

    pass


class AccountRecordExchangeResponse(BaseResponse):
    """Response model for currency exchange records.

    This model represents a currency exchange transaction, tracking
    exchange rates and converted amounts for financial records.

    Attributes:
        account_record_id (int): ID of the related account record
        account_record_uu_id (str): UUID of the related account record
        exchange_rate (Decimal): Applied exchange rate
        exchange_currency (str): Target currency code
        exchange_value (Decimal): Converted amount
        exchange_date (datetime): When the exchange was calculated
    """

    account_record_id: int
    account_record_uu_id: str
    exchange_rate: Decimal
    exchange_currency: str = "TRY"
    exchange_value: Decimal
    exchange_date: datetime


class AccountRecordExchangeCollection(CrudCollection[AccountRecordExchangeResponse]):
    """Collection of currency exchange records.

    This model represents a paginated list of currency exchange records
    with sorting and pagination information.
    """

    pass


class AccountRecordsListResponse(BaseModel):
    """Response model for account records list endpoint"""

    uu_id: UUID
    account_name: str
    account_code: str
    company_id: int
    company_uu_id: str
    created_at: datetime
    updated_at: Optional[datetime]
    deleted: bool = False
