"""
給与設定・税率・保険料のスキーマ定義 (Settings Schemas)
"""
from pydantic import BaseModel
from datetime import date, datetime
from typing import Optional
from decimal import Decimal


class SalarySettingBase(BaseModel):
    """給与基本設定"""
    employee_id: int
    base_salary: Decimal = Decimal("0")
    hourly_rate: Optional[Decimal] = None
    employment_type: str  # 正社員/契約社員/パート/アルバイト
    payment_type: str  # 月給/時給/日給
    commute_allowance: Decimal = Decimal("0")
    other_allowance: Decimal = Decimal("0")
    employment_insurance_eligible: bool = True  # 雇用保険加入対象
    valid_from: date
    valid_to: Optional[date] = None


class SalarySettingCreate(SalarySettingBase):
    """給与設定の作成"""
    pass


class SalarySettingUpdate(BaseModel):
    """給与設定の更新"""
    base_salary: Optional[Decimal] = None
    hourly_rate: Optional[Decimal] = None
    employment_type: Optional[str] = None
    payment_type: Optional[str] = None
    commute_allowance: Optional[Decimal] = None
    other_allowance: Optional[Decimal] = None
    employment_insurance_eligible: Optional[bool] = None
    valid_from: Optional[date] = None
    valid_to: Optional[date] = None


class SalarySetting(SalarySettingBase):
    """給与設定の情報"""
    setting_id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class InsuranceRateBase(BaseModel):
    """社会保険料率"""
    rate_year: int  # 適用年度（例: 2026）
    rate_type: str  # 健康保険/介護保険/厚生年金/雇用保険/労災保険
    calculation_target: str  # 給与/賞与
    prefecture: str = "東京都"  # 事業所所在地
    employee_rate: Decimal
    employer_rate: Decimal
    care_insurance_age_threshold: Optional[int] = None  # 介護保険適用年齢下限
    notes: Optional[str] = None


class InsuranceRateCreate(InsuranceRateBase):
    """保険料率の作成"""
    pass


class InsuranceRateUpdate(BaseModel):
    """保険料率の更新"""
    rate_year: Optional[int] = None
    rate_type: Optional[str] = None
    calculation_target: Optional[str] = None
    prefecture: Optional[str] = None
    employee_rate: Optional[Decimal] = None
    employer_rate: Optional[Decimal] = None
    care_insurance_age_threshold: Optional[int] = None
    notes: Optional[str] = None


class InsuranceRate(InsuranceRateBase):
    """保険料率の情報"""
    rate_id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class IncomeTaxTableBase(BaseModel):
    """所得税率表"""
    monthly_income_from: Decimal
    monthly_income_to: Decimal
    dependents_count: int = 0
    tax_amount: Decimal
    valid_from: date
    valid_to: Optional[date] = None


class IncomeTaxTableCreate(IncomeTaxTableBase):
    """所得税率表の作成"""
    pass


class IncomeTaxTable(IncomeTaxTableBase):
    """所得税率表の情報"""
    tax_id: int
    created_at: datetime

    class Config:
        from_attributes = True


# ========================================
# 子ども・子育て拠出金率
# ========================================

class ChildSupportContributionRateBase(BaseModel):
    """子ども・子育て拠出金率"""
    rate_year: int  # 適用年度
    income_threshold: Decimal  # 所得基準額（標準報酬月額）
    contribution_rate: Decimal  # 拠出金率（事業主負担のみ）
    notes: Optional[str] = None


class ChildSupportContributionRateCreate(ChildSupportContributionRateBase):
    """子ども・子育て拠出金率の作成"""
    pass


class ChildSupportContributionRateUpdate(BaseModel):
    """子ども・子育て拠出金率の更新"""
    income_threshold: Optional[Decimal] = None
    contribution_rate: Optional[Decimal] = None
    notes: Optional[str] = None


class ChildSupportContributionRate(ChildSupportContributionRateBase):
    """子ども・子育て拠出金率の情報"""
    contribution_id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


# ========================================
# 社会保険料上限設定
# ========================================

class InsuranceLimitSettingBase(BaseModel):
    """社会保険料上限設定"""
    setting_type: str  # 健康保険標準報酬月額上限/厚生年金標準報酬月額上限/通勤手当非課税限度額
    limit_amount: Decimal
    valid_from: date
    valid_to: Optional[date] = None
    notes: Optional[str] = None


class InsuranceLimitSettingCreate(InsuranceLimitSettingBase):
    """上限設定の作成"""
    pass


class InsuranceLimitSettingUpdate(BaseModel):
    """上限設定の更新"""
    limit_amount: Optional[Decimal] = None
    valid_from: Optional[date] = None
    valid_to: Optional[date] = None
    notes: Optional[str] = None


class InsuranceLimitSetting(InsuranceLimitSettingBase):
    """上限設定の情報"""
    limit_id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


# ========================================
# 扶養家族判定条件
# ========================================

class DependentRuleBase(BaseModel):
    """扶養家族判定条件"""
    rule_type: str  # 所得限度額/年齢条件/同一生計
    rule_value: str
    valid_from: date
    valid_to: Optional[date] = None
    description: Optional[str] = None


class DependentRuleCreate(DependentRuleBase):
    """判定条件の作成"""
    pass


class DependentRule(DependentRuleBase):
    """判定条件の情報"""
    rule_id: int
    created_at: datetime

    class Config:
        from_attributes = True

    """所得税率表の情報"""
    tax_id: int
    created_at: datetime

    class Config:
        from_attributes = True


class IncomeTaxImport(BaseModel):
    """所得税率表のインポート用"""
    data: list[IncomeTaxTableCreate]
    valid_from: date
