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

class CommercialAutoModel(SQLModel, table=True):
    __tablename__ = "commercial_auto_quotes"
    
    # Primary Key
    id: Optional[int] = Field(default=None, primary_key=True)
    user_id: Optional[int] = Field(default=None, foreign_key="users.id", nullable=True)
    
    # 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()
    
    # Vehicle Information
    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 = Field(default=False)
    
    # Driver Information
    driver_list: str = Field(max_length=2000)
    driver_experience_years: Optional[int] = Field(default=None, ge=0)
    drivers_under_25: bool = Field(default=False)
    mvr_violations_past_3_years: bool = Field(default=False)
    description_of_violations: Optional[str] = Field(default=None, max_length=1000)
    
    # Claims History
    accidents_claims_past_5_years: bool = Field(default=False)
    description_of_accidents_claims: Optional[str] = Field(default=None, max_length=1000)
    
    # Current Insurance
    current_auto_insurance: Optional[bool] = Field(default=None)
    current_carrier: Optional[str] = Field(default=None, max_length=255)
    current_policy_expiration_date: Optional[date] = Field(default=None)
    
    # Coverage Preferences
    desired_coverage_types: str = Field(max_length=500)
    desired_coverage_limits: Decimal = Field()
    comprehensive_collision_deductibles: Optional[Decimal] = Field(default=None)
    hired_non_owned_coverage_needed: Optional[bool] = Field(default=None)
    
    # Special Equipment
    special_equipment_on_vehicles: Optional[bool] = Field(default=None)
    description_of_equipment: Optional[str] = Field(default=None, max_length=1000)
    
    # Policy Details
    desired_policy_start_date: date
    broker_agent_name: Optional[str] = Field(default=None, max_length=255)
    notes_special_requests: Optional[str] = Field(default=None, max_length=1000)
    
    # System Fields
    quote_status: str = Field(default="pending", max_length=50)
    premium_amount: Optional[Decimal] = Field(default=None)
    created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
    updated_at: Optional[datetime] = Field(default=None)
    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)
