from pydantic import BaseModel, Field
from typing import Optional
from datetime import date, datetime
from decimal import Decimal


class ProjectBase(BaseModel):
    project_no: str = Field(..., max_length=50)
    name: str = Field(..., max_length=200)
    engineering_type: str = Field(..., max_length=50)
    contract_amount: Optional[Decimal] = Field(None, ge=0)


class ProjectCreate(ProjectBase):
    power_contract_no: Optional[str] = Field(None, max_length=100)
    subitem_count: Optional[int] = Field(None, ge=0)
    subitem_code: Optional[str] = Field(None, max_length=50)
    total_investment: Optional[Decimal] = Field(None, ge=0)
    warranty_ratio: Optional[Decimal] = None
    settlement_amount: Optional[Decimal] = None
    total_cost_estimated: Optional[Decimal] = None
    voltage_level: Optional[str] = Field(None, max_length=50)
    owner_unit: Optional[str] = Field(None, max_length=200)
    owner_contact: Optional[str] = Field(None, max_length=200)
    bidding_type: Optional[str] = Field(None, max_length=50)
    signing_date: Optional[date] = None
    start_date: Optional[date] = None
    planned_end_date: Optional[date] = None
    project_department: Optional[str] = Field(None, max_length=100)
    project_leader: Optional[str] = Field(None, max_length=200)
    payment_method: Optional[str] = None
    total_cost_control: Optional[Decimal] = None
    labor_cost_control: Optional[Decimal] = None
    material_cost_control: Optional[Decimal] = None
    other_cost_control: Optional[Decimal] = None
    remarks: Optional[str] = None


class ProjectUpdate(BaseModel):
    name: Optional[str] = Field(None, max_length=200)
    engineering_type: Optional[str] = Field(None, max_length=50)
    contract_amount: Optional[Decimal] = Field(None, ge=0)
    total_investment: Optional[Decimal] = None
    settlement_amount: Optional[Decimal] = None
    actual_receipt_amount: Optional[Decimal] = None
    actual_payment_amount: Optional[Decimal] = None
    invoice_amount: Optional[Decimal] = None
    payable_amount: Optional[Decimal] = None
    cumulative_progress: Optional[Decimal] = None
    labor_cost_paid: Optional[Decimal] = None
    material_cost_actual: Optional[Decimal] = None
    material_cost_paid: Optional[Decimal] = None
    other_cost_actual: Optional[Decimal] = None
    tax_amount: Optional[Decimal] = None
    profit: Optional[Decimal] = None
    remarks: Optional[str] = None
    problems: Optional[str] = None
    suggestions: Optional[str] = None


class ProjectResponse(BaseModel):
    id: int
    project_no: str
    power_contract_no: Optional[str]
    name: str
    subitem_count: Optional[int]
    subitem_code: Optional[str]
    total_investment: Optional[Decimal]
    contract_amount: Optional[Decimal]
    warranty_ratio: Optional[Decimal]
    settlement_amount: Optional[Decimal]
    total_cost_estimated: Optional[Decimal]
    voltage_level: Optional[str]
    engineering_type: Optional[str]
    owner_unit: Optional[str]
    owner_contact: Optional[str]
    bidding_type: Optional[str]
    signing_date: Optional[date]
    start_date: Optional[date]
    planned_end_date: Optional[date]
    actual_end_date: Optional[date]
    warranty_amount: Optional[Decimal]
    warranty_expiry_date: Optional[date]
    actual_warranty_refund_date: Optional[date]
    project_department: Optional[str]
    project_leader: Optional[str]
    payment_method: Optional[str]
    total_cost_control: Optional[Decimal]
    is_adjusted: Optional[str]
    labor_cost_control: Optional[Decimal]
    labor_cost_planned: Optional[Decimal]
    labor_cost_paid: Optional[Decimal]
    material_cost_control: Optional[Decimal]
    material_cost_payable: Optional[Decimal]
    material_cost_actual: Optional[Decimal]
    material_cost_paid: Optional[Decimal]
    other_cost_control: Optional[Decimal]
    other_cost_payable: Optional[Decimal]
    other_cost_actual: Optional[Decimal]
    tax_amount: Optional[Decimal]
    profit: Optional[Decimal]
    actual_profit: Optional[Decimal]
    cost_settlement_amount: Optional[Decimal]
    cumulative_progress: Optional[Decimal]
    receivable_amount: Optional[Decimal]
    invoice_amount: Optional[Decimal]
    actual_receipt_amount: Optional[Decimal]
    receipt_completion_rate: Optional[Decimal]
    payable_amount: Optional[Decimal]
    actual_payment_amount: Optional[Decimal]
    unpaid_amount: Optional[Decimal]
    payment_completion_rate: Optional[Decimal]
    labor_debt_amount: Optional[Decimal]
    settlement_cost_amount: Optional[Decimal]
    settlement_labor_cost: Optional[Decimal]
    settlement_material_cost: Optional[Decimal]
    settlement_other_cost: Optional[Decimal]
    due_settlement_count: Optional[int]
    unsettlement_count: Optional[int]
    problems: Optional[str]
    suggestions: Optional[str]
    remarks: Optional[str]
    created_by: int
    created_by_name: Optional[str] = None
    created_at: datetime
    updated_at: Optional[datetime]

    class Config:
        from_attributes = True


class ProjectListResponse(BaseModel):
    items: list[ProjectResponse]
    total: int
    page: int
    page_size: int
