from sqlmodel import SQLModel, Field
from typing import Optional
from datetime import datetime, date, timezone
from decimal import Decimal
from app.services.encryption_service import encryption_service
from app.core.base_model import BaseInsuranceModel

class CommercialPropertyModel(BaseInsuranceModel, table=True):
    __tablename__ = "commercial_property_quotes"
    
    # Business Information (encrypted fields have larger max_length)
    business_name: str = Field(max_length=500)
    contact_name: str = Field(max_length=500)
    email_address: str = Field(max_length=500)
    phone_number: str = Field(max_length=100)
    business_address: str = Field(max_length=1000)
    type_of_business: str = Field(max_length=255)
    business_entity_type: Optional[str] = Field(default=None, max_length=100)
    years_in_business: int = Field(ge=0)
    annual_revenue: Decimal = Field()
    number_of_employees: int = Field(ge=1)
    number_of_locations: int = Field(ge=1)
    
    # Property Information
    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()
    contents_inventory_value: Decimal = Field()
    
    # Coverage Information
    business_interruption_coverage_needed: Optional[bool] = Field(default=None)
    business_income_annual: Optional[Decimal] = Field(default=None)
    security_features: Optional[str] = Field(default=None, max_length=500)
    distance_to_fire_hydrant_ft: Optional[int] = Field(default=None, ge=0)
    distance_to_fire_station_miles: Optional[float] = Field(default=None, ge=0)
    
    # Existing Insurance
    existing_property_insurance: Optional[bool] = Field(default=None)
    current_carrier: Optional[str] = Field(default=None, max_length=255)
    policy_expiration_date: Optional[date] = Field(default=None)
    prior_claims_last_5_years: bool
    description_of_past_claims: Optional[str] = Field(default=None, max_length=2000)
    
    # Policy Preferences
    desired_coverage_start_date: date
    desired_deductible: Optional[Decimal] = Field(default=None)
    desired_coverage_limit: Decimal = Field()
    special_requirements_notes: Optional[str] = Field(default=None, max_length=2000)
    transaction_no: str = Field(index=True, max_length=100, description="Transaction number")
    lead_source: Optional[str] = Field(default=None, max_length=100, description="Where the user heard about us")
    
    def encrypt_sensitive_data(self):
        """Encrypt sensitive fields before saving"""
        self.business_name = encryption_service.encrypt_field(self.business_name)
        self.contact_name = encryption_service.encrypt_field(self.contact_name)
        self.email_address = encryption_service.encrypt_field(self.email_address)
        self.phone_number = encryption_service.encrypt_field(self.phone_number)
        self.business_address = encryption_service.encrypt_field(self.business_address)
    
    def decrypt_sensitive_data(self):
        """Decrypt sensitive fields after loading"""
        self.business_name = encryption_service.decrypt_field(self.business_name)
        self.contact_name = encryption_service.decrypt_field(self.contact_name)
        self.email_address = encryption_service.decrypt_field(self.email_address)
        self.phone_number = encryption_service.decrypt_field(self.phone_number)
        self.business_address = encryption_service.decrypt_field(self.business_address)

    def get_encrypted_fields(self):
        return ["business_name", "contact_name", "email_address", "phone_number", "business_address"]
   
    def get_searchable_fields(self):
        return ["business_name", "contact_name", "email_address", "type_of_business", "quote_status"]
 