from pydantic import BaseModel, EmailStr, Field, field_validator, computed_field
from typing import Optional, List
from datetime import date, datetime
from decimal import Decimal
from app.utils.masking import mask_email, mask_phone
from app.utils.validation_helpers import clean_phone
import re

class VehicleInfo(BaseModel):
    year: int = Field(..., ge=1900, le=2030)
    make: str = Field(..., max_length=50)
    model: str = Field(..., max_length=50)
    vin: str = Field(..., min_length=17, max_length=17)

class DriverInfo(BaseModel):
    name: str = Field(..., max_length=100)
    date_of_birth: date
    license_number: str = Field(..., max_length=50)
    license_state: str = Field(..., max_length=2)

class CommercialAutoBase(BaseModel):
    business_name: str = Field(..., max_length=255)
    contact_name: str = Field(..., max_length=255)
    email_address: EmailStr
    phone_number: str = Field(..., max_length=20)
    business_address: str = Field(..., max_length=500)
    type_of_business: str = Field(..., max_length=255)
    business_entity_type: Optional[str] = Field(None, max_length=100)
    years_in_business: int = Field(..., ge=0)
    annual_revenue: Decimal = Field(..., decimal_places=2)
    
    number_of_vehicles: int = Field(..., ge=1)
    vehicle_list: str = Field(..., max_length=2000)
    vehicle_use_type: str = Field(..., max_length=100)
    radius_of_operation: str = Field(..., max_length=100)
    garaging_zip_codes: str = Field(..., max_length=500)
    vehicles_cross_state_lines: bool
    
    driver_list: str = Field(..., max_length=2000)
    driver_experience_years: Optional[int] = Field(None, ge=0)
    drivers_under_25: bool
    mvr_violations_past_3_years: bool
    description_of_violations: Optional[str] = Field(None, max_length=1000)
    
    accidents_claims_past_5_years: bool
    description_of_accidents_claims: Optional[str] = Field(None, max_length=1000)
    
    current_auto_insurance: Optional[bool] = None
    current_carrier: Optional[str] = Field(None, max_length=255)
    current_policy_expiration_date: Optional[date] = None
    
    desired_coverage_types: str = Field(..., max_length=500)
    desired_coverage_limits: Decimal = Field(..., decimal_places=2)
    comprehensive_collision_deductibles: Optional[Decimal] = Field(None, decimal_places=2)
    hired_non_owned_coverage_needed: Optional[bool] = None
    
    special_equipment_on_vehicles: Optional[bool] = None
    description_of_equipment: Optional[str] = Field(None, max_length=1000)
    
    desired_policy_start_date: date
    broker_agent_name: Optional[str] = Field(None, max_length=255)
    notes_special_requests: Optional[str] = Field(None, max_length=1000)
    transaction_no: str
    lead_source: Optional[str] = None

    @field_validator('phone_number', 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

class CommercialAutoCreate(CommercialAutoBase):
    pass

class CommercialAutoUpdate(BaseModel):
    business_name: Optional[str] = Field(None, max_length=255)
    contact_name: Optional[str] = Field(None, max_length=255)
    email_address: Optional[EmailStr] = None
    phone_number: Optional[str] = Field(None, max_length=20)
    business_address: Optional[str] = Field(None, max_length=500)
    type_of_business: Optional[str] = Field(None, max_length=255)
    business_entity_type: Optional[str] = Field(None, max_length=100)
    years_in_business: Optional[int] = Field(None, ge=0)
    annual_revenue: Optional[Decimal] = Field(None, decimal_places=2)
    
    number_of_vehicles: Optional[int] = Field(None, ge=1)
    vehicle_list: Optional[str] = Field(None, max_length=2000)
    vehicle_use_type: Optional[str] = Field(None, max_length=100)
    radius_of_operation: Optional[str] = Field(None, max_length=100)
    garaging_zip_codes: Optional[str] = Field(None, max_length=500)
    vehicles_cross_state_lines: Optional[bool] = None
    
    driver_list: Optional[str] = Field(None, max_length=2000)
    driver_experience_years: Optional[int] = Field(None, ge=0)
    drivers_under_25: Optional[bool] = None
    mvr_violations_past_3_years: Optional[bool] = None
    description_of_violations: Optional[str] = Field(None, max_length=1000)
    
    accidents_claims_past_5_years: Optional[bool] = None
    description_of_accidents_claims: Optional[str] = Field(None, max_length=1000)
    
    current_auto_insurance: Optional[bool] = None
    current_carrier: Optional[str] = Field(None, max_length=255)
    current_policy_expiration_date: Optional[date] = None
    
    desired_coverage_types: Optional[str] = Field(None, max_length=500)
    desired_coverage_limits: Optional[Decimal] = Field(None, decimal_places=2)
    comprehensive_collision_deductibles: Optional[Decimal] = Field(None, decimal_places=2)
    hired_non_owned_coverage_needed: Optional[bool] = None
    
    special_equipment_on_vehicles: Optional[bool] = None
    description_of_equipment: Optional[str] = Field(None, max_length=1000)
    
    desired_policy_start_date: Optional[date] = None
    broker_agent_name: Optional[str] = Field(None, max_length=255)
    notes_special_requests: Optional[str] = Field(None, max_length=1000)
    lead_source: Optional[str] = None
    
    quote_status: Optional[str] = Field(None, max_length=50)
    premium_amount: Optional[Decimal] = Field(None, decimal_places=2)

class CommercialAutoResponse(CommercialAutoBase):
    id: int
    quote_status: str
    premium_amount: Optional[Decimal]
    created_at: datetime
    updated_at: Optional[datetime]
    
    @computed_field
    @property
    def masked_email(self) -> str:
        return mask_email(self.email_address)
    
    @computed_field
    @property
    def masked_phone(self) -> str:
        return mask_phone(self.phone_number)
    
    model_config = {"from_attributes": True}