from pydantic import BaseModel, EmailStr, Field, field_validator
from typing import Optional
from datetime import date, datetime
from decimal import Decimal
from app.core.base_schema import BaseInsuranceResponse, BaseInsuranceUpdate
import re

class CommercialPropertyBase(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_employees: int = Field(..., ge=1)
    number_of_locations: int = Field(..., ge=1)
    
    property_addresses: str = Field(..., max_length=2000)
    ownership_status: str = Field(..., max_length=50)
    building_use: str = Field(..., max_length=100)
    building_construction_type: str = Field(..., max_length=100)
    year_built: int = Field(..., ge=1800, le=2030)
    square_footage: int = Field(..., gt=0)
    building_replacement_value: Decimal = Field(..., decimal_places=2)
    contents_inventory_value: Decimal = Field(..., decimal_places=2)
    
    business_interruption_coverage_needed: Optional[bool] = None
    business_income_annual: Optional[Decimal] = Field(None, decimal_places=2)
    security_features: Optional[str] = Field(None, max_length=500)
    distance_to_fire_hydrant_ft: Optional[int] = Field(None, ge=0)
    distance_to_fire_station_miles: Optional[float] = Field(None, ge=0)
    
    existing_property_insurance: Optional[bool] = None
    current_carrier: Optional[str] = Field(None, max_length=255)
    policy_expiration_date: Optional[date] = None
    prior_claims_last_5_years: bool
    description_of_past_claims: Optional[str] = Field(None, max_length=2000)
    
    desired_coverage_start_date: date
    desired_deductible: Optional[Decimal] = Field(None, decimal_places=2)
    desired_coverage_limit: Decimal = Field(..., decimal_places=2)
    special_requirements_notes: Optional[str] = Field(None, max_length=2000)
    lead_source: Optional[str] = None

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

    @field_validator('description_of_past_claims')
    @classmethod
    def validate_claims_description(cls, v, values):
        if values.data.get('prior_claims_last_5_years') and not v:
            raise ValueError('Description of past claims is required when claims = True')
        return v

class CommercialPropertyCreate(CommercialPropertyBase):
    pass

class CommercialPropertyUpdate(BaseInsuranceUpdate):
    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_employees: Optional[int] = Field(None, ge=1)
    number_of_locations: Optional[int] = Field(None, ge=1)
    
    property_addresses: Optional[str] = Field(None, max_length=2000)
    ownership_status: Optional[str] = Field(None, max_length=50)
    building_use: Optional[str] = Field(None, max_length=100)
    building_construction_type: Optional[str] = Field(None, max_length=100)
    year_built: Optional[int] = Field(None, ge=1800, le=2030)
    square_footage: Optional[int] = Field(None, gt=0)
    building_replacement_value: Optional[Decimal] = Field(None, decimal_places=2)
    contents_inventory_value: Optional[Decimal] = Field(None, decimal_places=2)
    
    business_interruption_coverage_needed: Optional[bool] = None
    business_income_annual: Optional[Decimal] = Field(None, decimal_places=2)
    security_features: Optional[str] = Field(None, max_length=500)
    distance_to_fire_hydrant_ft: Optional[int] = Field(None, ge=0)
    distance_to_fire_station_miles: Optional[float] = Field(None, ge=0)
    
    existing_property_insurance: Optional[bool] = None
    current_carrier: Optional[str] = Field(None, max_length=255)
    policy_expiration_date: Optional[date] = None
    prior_claims_last_5_years: Optional[bool] = None
    description_of_past_claims: Optional[str] = Field(None, max_length=2000)
    
    desired_coverage_start_date: Optional[date] = None
    desired_deductible: Optional[Decimal] = Field(None, decimal_places=2)
    desired_coverage_limit: Optional[Decimal] = Field(None, decimal_places=2)
    special_requirements_notes: Optional[str] = Field(None, max_length=2000)
    lead_source: Optional[str] = None

class CommercialPropertyResponse(CommercialPropertyBase, BaseInsuranceResponse):
    pass