"""
月次給与計算のスキーマ定義 (Payroll Calculation Schemas)
"""
from pydantic import BaseModel
from datetime import date, datetime
from typing import Optional
from decimal import Decimal


class PayrollDetailBase(BaseModel):
    """給与明細項目"""
    item_type: str  # payment/deduction
    item_code: str
    item_name: str
    amount: Decimal
    notes: Optional[str] = None


class PayrollDetailCreate(PayrollDetailBase):
    """給与明細項目の作成"""
    pass


class PayrollDetail(PayrollDetailBase):
    """給与明細項目の情報"""
    detail_id: int
    payroll_id: int
    created_at: datetime

    class Config:
        from_attributes = True


class MonthlyPayrollBase(BaseModel):
    """月次給与計算の基本情報"""
    employee_id: int
    payroll_year: int
    payroll_month: int
    payment_date: date
    
    # 勤怠情報
    working_days: Decimal = Decimal("0")
    working_hours: Decimal = Decimal("0")
    overtime_hours: Decimal = Decimal("0")
    holiday_hours: Decimal = Decimal("0")
    late_hours: Decimal = Decimal("0")
    absent_days: Decimal = Decimal("0")
    
    notes: Optional[str] = None


class MonthlyPayrollCreate(MonthlyPayrollBase):
    """月次給与計算の作成"""
    pass


class MonthlyPayrollUpdate(BaseModel):
    """月次給与計算の更新"""
    payment_date: Optional[date] = None
    working_days: Optional[Decimal] = None
    working_hours: Optional[Decimal] = None
    overtime_hours: Optional[Decimal] = None
    holiday_hours: Optional[Decimal] = None
    late_hours: Optional[Decimal] = None
    absent_days: Optional[Decimal] = None
    notes: Optional[str] = None


class MonthlyPayroll(MonthlyPayrollBase):
    """月次給与計算の情報"""
    payroll_id: int
    status: str
    
    # 支給項目
    base_salary: Decimal
    overtime_pay: Decimal
    holiday_pay: Decimal
    commute_allowance: Decimal
    other_allowance: Decimal
    total_payment: Decimal
    
    # 控除項目
    health_insurance: Decimal
    care_insurance: Decimal
    pension_insurance: Decimal
    employment_insurance: Decimal
    income_tax: Decimal
    resident_tax: Decimal
    other_deduction: Decimal
    total_deduction: Decimal
    
    # 差引支給額
    net_payment: Decimal
    
    calculated_at: Optional[datetime] = None
    calculated_by: Optional[str] = None
    approved_at: Optional[datetime] = None
    approved_by: Optional[str] = None
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class MonthlyPayrollWithDetails(MonthlyPayroll):
    """明細項目を含む月次給与情報"""
    details: list[PayrollDetail] = []


class PayrollCalculationRequest(BaseModel):
    """給与計算リクエスト"""
    employee_id: int
    payroll_year: int
    payroll_month: int
    working_days: Decimal = Decimal("0")
    working_hours: Decimal = Decimal("0")
    overtime_hours: Decimal = Decimal("0")
    holiday_hours: Decimal = Decimal("0")
    late_hours: Decimal = Decimal("0")
    absent_days: Decimal = Decimal("0")
    payment_date: date
    other_allowance: Decimal = Decimal("0")
    resident_tax: Decimal = Decimal("0")
    other_deduction: Decimal = Decimal("0")
    calc_income_tax: bool = True
    calc_social_insurance: bool = True


class RecalculateRequest(BaseModel):
    """再計算リクエスト"""
    calc_income_tax: bool = True
    calc_social_insurance: bool = True


class PayrollApprovalRequest(BaseModel):
    """給与承認リクエスト"""
    approved_by: str
