from sqlmodel import SQLModel, Field
from typing import Optional
from datetime import date, datetime
from enum import Enum
from decimal import Decimal

class PropertyType(str, Enum):
    single_family = "Single Family"
    condo = "Condo"
    townhouse = "Townhouse"
    rental = "Rental"
    multi_family = "Multi-Family"

class ConstructionType(str, Enum):
    frame = "Frame"
    masonry = "Masonry"
    brick = "Brick"
    steel = "Steel"
    other = "Other"

class RoofType(str, Enum):
    asphalt = "Asphalt"
    metal = "Metal"
    tile = "Tile"
    flat = "Flat"
    other = "Other"

class HeatingType(str, Enum):
    gas = "Gas"
    electric = "Electric"
    oil = "Oil"
    propane = "Propane"
    other = "Other"

class OccupancyType(str, Enum):
    owner = "Owner"
    tenant = "Tenant"
    seasonal = "Seasonal"
    vacant = "Vacant"
    under_construction = "Under Construction"

class CoverageLevel(str, Enum):
    basic = "Basic"
    standard = "Standard"
    comprehensive = "Comprehensive"

US_STATES = {
    "AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IA",
    "KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ",
    "NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT",
    "VA","WA","WV","WI","WY"
}

class HomeInsurance(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    user_id: Optional[int] = Field(default=None, foreign_key="users.id", nullable=True)
    
    # Personal info
    first_name: str = Field(..., regex="^[A-Za-z]+$", description="Legal first name")
    last_name: str = Field(..., regex="^[A-Za-z]+$", description="Legal last name")
    date_of_birth: date = Field(..., description="Date of Birth, must be ≥18 years")
    phone_number: str = Field(..., regex="^[0-9]{10,15}$", description="Phone number 10-15 digits")
    email: str = Field(..., description="Valid email address")
    
    # Property info
    property_address: str = Field(..., description="Street address")
    city: str = Field(..., description="City")
    state: str = Field(..., description="2-letter US state code")
    zip_code: str = Field(..., regex="^[0-9]{5}$", description="5-digit ZIP code")
    property_type: PropertyType = Field(..., description="Type of property")
    year_built: int = Field(..., ge=1800, le=datetime.now().year, description="Year built")
    square_footage: int = Field(..., gt=300, description="Square footage")
    construction_type: ConstructionType = Field(..., description="Construction type")
    roof_type: Optional[RoofType] = Field(None, description="Roof type")
    roof_year_replaced: Optional[int] = Field(None, le=datetime.now().year, description="Year roof replaced")
    number_of_stories: Optional[int] = Field(None, ge=1, le=4, description="Number of stories")
    number_of_bedrooms: Optional[int] = Field(None, ge=1, description="Number of bedrooms")
    number_of_bathrooms: Optional[int] = Field(None, ge=1, description="Number of bathrooms")
    heating_type: Optional[HeatingType] = Field(None, description="Heating type")
    
    # Other details
    primary_residence: bool = Field(..., description="Is primary residence?")
    occupancy_type: OccupancyType = Field(..., description="Occupancy type")
    home_security_system: Optional[bool] = Field(None, description="Home security system installed?")
    fire_alarm_smoke_detector: Optional[bool] = Field(None, description="Fire/smoke detectors installed?")
    swimming_pool: Optional[bool] = Field(None, description="Swimming pool present?")
    trampoline: Optional[bool] = Field(None, description="Trampoline present?")
    pets: Optional[bool] = Field(None, description="Pets on property?")
    estimated_replacement_cost: Optional[Decimal] = Field(None, description="Estimated replacement cost")
    mortgage_lender_name: Optional[str] = Field(None, description="Mortgage lender name")
    prior_claims_last_5_years: Optional[int] = Field(None, ge=0, description="Prior claims in last 5 years")
    current_insurance_provider: Optional[str] = Field(None, description="Current insurance provider")
    
    # Coverage info
    desired_coverage_start_date: date = Field(..., description="Desired coverage start date")
    preferred_coverage_level: Optional[CoverageLevel] = Field(None, description="Preferred coverage level")
    quote_status: str = Field(default="Draft", max_length=20, description="Quote status")
    lead_source: Optional[str] = Field(default=None, description="Where did you hear about us?")
    transaction_no: str = Field(index=True, max_length=100, description="Transaction number")
