from pydantic import BaseModel, Field, field_validator, EmailStr
from typing import Optional
from datetime import date, datetime
from app.core.base_schema import BaseInsuranceResponse, BaseInsuranceUpdate
from app.utils.validation_helpers import clean_phone, clean_state
import re

class UmbrellaInsuranceCreate(BaseModel):
    # Personal Details
    full_name: str = Field(..., min_length=1, max_length=200)
    date_of_birth: date
    email_address: EmailStr
    mobile_phone: str = Field(..., max_length=20)
    residential_address: str = Field(..., min_length=1, max_length=500)
    state_of_residence: str = Field(..., min_length=1)
    
    # Property & Assets
    home_ownership: str = Field(..., min_length=1)
    homeowner_renters_provider: Optional[str] = Field(None, max_length=100)
    owns_rental_property: bool
    number_of_vehicles: int = Field(..., ge=0, le=10)
    auto_insurance_provider: Optional[str] = Field(None, max_length=100)
    owns_boat_atv_rv: bool
    
    # Household Risk Factors
    teen_drivers_in_home: bool
    high_risk_pets: bool
    liability_claims_last_5_years: bool
    
    # Financial Overview
    annual_household_income: float = Field(..., gt=0)
    total_net_worth: float
    desired_coverage_amount: str = Field(..., min_length=1)
    
    # Review & Submit
    preferred_contact_method: str = Field(..., min_length=1)
    additional_notes: Optional[str] = Field(None, max_length=2000)
    lead_source: Optional[str] = Field(None, max_length=100)

    @field_validator('mobile_phone', mode='before')
    @classmethod
    def validate_phone(cls, v):
        if v:
            v = clean_phone(v)
            pattern = r'^\(\d{3}\) \d{3}-\d{4}$'
            if not re.match(pattern, v):
                raise ValueError('Phone number must be in format (123) 456-7890')
        return v

    @field_validator('state_of_residence', mode='before')
    @classmethod
    def validate_state(cls, v):
        if v:
            return clean_state(v)
        return v

    @field_validator('date_of_birth')
    @classmethod
    def validate_age(cls, v):
        today = date.today()
        age = today.year - v.year - ((today.month, today.day) < (v.month, v.day))
        if age < 18:
            raise ValueError('Must be at least 18 years old')
        return v

class UmbrellaInsuranceResponse(BaseInsuranceResponse):
    # Personal Details
    full_name: str
    date_of_birth: date
    email_address: str
    mobile_phone: str
    residential_address: str
    state_of_residence: str
    
    # Property & Assets
    home_ownership: str
    homeowner_renters_provider: Optional[str]
    owns_rental_property: bool
    number_of_vehicles: int
    auto_insurance_provider: Optional[str]
    owns_boat_atv_rv: bool
    
    # Household Risk Factors
    teen_drivers_in_home: bool
    high_risk_pets: bool
    liability_claims_last_5_years: bool
    
    # Financial Overview
    annual_household_income: float
    total_net_worth: float
    desired_coverage_amount: str
    
    # Review & Submit
    preferred_contact_method: str
    additional_notes: Optional[str]
    lead_source: Optional[str] = None

class UmbrellaInsuranceUpdate(BaseInsuranceUpdate):
    # Personal Details
    full_name: Optional[str] = Field(None, min_length=1, max_length=200)
    date_of_birth: Optional[date] = None
    email_address: Optional[EmailStr] = None
    mobile_phone: Optional[str] = Field(None, max_length=20)
    residential_address: Optional[str] = Field(None, min_length=1, max_length=500)
    state_of_residence: Optional[str] = Field(None, min_length=1)
    
    # Property & Assets
    home_ownership: Optional[str] = Field(None, min_length=1)
    homeowner_renters_provider: Optional[str] = Field(None, max_length=100)
    owns_rental_property: Optional[bool] = None
    number_of_vehicles: Optional[int] = Field(None, ge=0, le=10)
    auto_insurance_provider: Optional[str] = Field(None, max_length=100)
    owns_boat_atv_rv: Optional[bool] = None
    
    # Household Risk Factors
    teen_drivers_in_home: Optional[bool] = None
    high_risk_pets: Optional[bool] = None
    liability_claims_last_5_years: Optional[bool] = None
    
    # Financial Overview
    annual_household_income: Optional[float] = Field(None, gt=0)
    total_net_worth: Optional[float] = None
    desired_coverage_amount: Optional[str] = Field(None, min_length=1)
    
    # Review & Submit
    preferred_contact_method: Optional[str] = Field(None, min_length=1)
    additional_notes: Optional[str] = Field(None, max_length=2000)