"""Nomenclature models for synced and local data."""

from typing import Optional
from datetime import datetime
from sqlmodel import SQLModel, Field


class SyncedSupplier(SQLModel, table=True):
    """Suppliers synced from Oracle NOM_PARTENERI."""
    __tablename__ = "synced_suppliers"

    id: Optional[int] = Field(default=None, primary_key=True)
    oracle_id: int = Field(index=True)  # Original Oracle ID
    company_id: int = Field(index=True)  # Company this supplier belongs to
    name: str = Field(max_length=200)
    fiscal_code: Optional[str] = Field(default=None, max_length=50, index=True)  # CUI/CIF
    address: Optional[str] = Field(default=None, max_length=500)
    synced_at: datetime = Field(default_factory=datetime.utcnow)


class LocalSupplier(SQLModel, table=True):
    """Suppliers created locally from OCR (not in Oracle)."""
    __tablename__ = "local_suppliers"

    id: Optional[int] = Field(default=None, primary_key=True)
    company_id: int = Field(index=True)
    name: str = Field(max_length=200)
    fiscal_code: Optional[str] = Field(default=None, max_length=50, index=True)
    address: Optional[str] = Field(default=None, max_length=500)
    created_by: str = Field(max_length=100)  # Username who created it
    created_at: datetime = Field(default_factory=datetime.utcnow)
    # Flag to indicate if it should be synced to Oracle later
    pending_oracle_sync: bool = Field(default=True)


class SyncedCashRegister(SQLModel, table=True):
    """Cash registers and bank accounts synced from Oracle."""
    __tablename__ = "synced_cash_registers"

    id: Optional[int] = Field(default=None, primary_key=True)
    oracle_id: int = Field(index=True)
    company_id: int = Field(index=True)
    name: str = Field(max_length=100)
    account_code: str = Field(max_length=20)  # 5311, 5121, etc.
    register_type: str = Field(max_length=10)  # 'cash' or 'bank'
    synced_at: datetime = Field(default_factory=datetime.utcnow)
