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 OceanMarineModel(BaseInsuranceModel, table=True):
    __tablename__ = "ocean_marine_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)
    
    # Operation Information
    type_of_operation: str = Field(max_length=100)
    description_of_goods: str = Field(max_length=2000)
    value_per_shipment: Decimal = Field(ge=0)
    annual_cargo_value: Decimal = Field(ge=0)
    
    # Shipping Information
    shipping_methods: str = Field(max_length=500)  # Comma-separated
    countries_of_origin: str = Field(max_length=1000)  # Comma-separated
    countries_of_destination: str = Field(max_length=1000)  # Comma-separated
    packaging_type: Optional[str] = Field(default=None, max_length=100)
    vessel_information: Optional[str] = Field(default=None, max_length=1000)
    own_vessel: bool = Field(default=False)
    
    # Shipment Details
    number_of_shipments_per_year: int = Field(ge=1)
    max_value_single_shipment: Decimal = Field(ge=0)
    past_claims_history: Optional[str] = Field(default=None, max_length=2000)
    
    # Policy Information
    desired_policy_start_date: date
    desired_coverage_types: Optional[str] = Field(default=None, max_length=500)  # Comma-separated
    deductible_preference: Optional[Decimal] = Field(default=None)
    certificate_insurance_needed: Optional[bool] = Field(default=None)
    additional_insured_parties: Optional[str] = Field(default=None, max_length=1000)
    special_coverage_requirements: Optional[str] = Field(default=None, max_length=2000)
    transaction_no: str = Field(index=True, max_length=100, description="Transaction number")
    
    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"]