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
from app.utils.validation_helpers import clean_phone
import re

class OceanMarineBase(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_operation: str = Field(..., max_length=100)
    description_of_goods: str = Field(..., max_length=2000)
    value_per_shipment: Decimal = Field(..., decimal_places=2, ge=0)
    annual_cargo_value: Decimal = Field(..., decimal_places=2, ge=0)
    
    shipping_methods: str = Field(..., max_length=500)
    countries_of_origin: str = Field(..., max_length=1000)
    countries_of_destination: str = Field(..., max_length=1000)
    packaging_type: Optional[str] = Field(None, max_length=100)
    vessel_information: Optional[str] = Field(None, max_length=1000)
    own_vessel: bool
    
    number_of_shipments_per_year: int = Field(..., ge=1)
    max_value_single_shipment: Decimal = Field(..., decimal_places=2, ge=0)
    past_claims_history: Optional[str] = Field(None, max_length=2000)
    
    desired_policy_start_date: date
    desired_coverage_types: Optional[str] = Field(None, max_length=500)
    deductible_preference: Optional[Decimal] = Field(None, decimal_places=2)
    certificate_insurance_needed: Optional[bool] = None
    additional_insured_parties: Optional[str] = Field(None, max_length=1000)
    special_coverage_requirements: Optional[str] = Field(None, max_length=2000)

    @field_validator('phone_number', mode='before')
    @classmethod
    def validate_phone(cls, v):
        if v:
            v = clean_phone(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('desired_policy_start_date')
    @classmethod
    def validate_future_date(cls, v):
        if v <= date.today():
            raise ValueError('Desired policy start date must be in the future')
        return v

class OceanMarineCreate(OceanMarineBase):
    pass

class OceanMarineUpdate(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_operation: Optional[str] = Field(None, max_length=100)
    description_of_goods: Optional[str] = Field(None, max_length=2000)
    value_per_shipment: Optional[Decimal] = Field(None, decimal_places=2, ge=0)
    annual_cargo_value: Optional[Decimal] = Field(None, decimal_places=2, ge=0)
    
    shipping_methods: Optional[str] = Field(None, max_length=500)
    countries_of_origin: Optional[str] = Field(None, max_length=1000)
    countries_of_destination: Optional[str] = Field(None, max_length=1000)
    packaging_type: Optional[str] = Field(None, max_length=100)
    vessel_information: Optional[str] = Field(None, max_length=1000)
    own_vessel: Optional[bool] = None
    
    number_of_shipments_per_year: Optional[int] = Field(None, ge=1)
    max_value_single_shipment: Optional[Decimal] = Field(None, decimal_places=2, ge=0)
    past_claims_history: Optional[str] = Field(None, max_length=2000)
    
    desired_policy_start_date: Optional[date] = None
    desired_coverage_types: Optional[str] = Field(None, max_length=500)
    deductible_preference: Optional[Decimal] = Field(None, decimal_places=2)
    certificate_insurance_needed: Optional[bool] = None
    additional_insured_parties: Optional[str] = Field(None, max_length=1000)
    special_coverage_requirements: Optional[str] = Field(None, max_length=2000)

class OceanMarineResponse(OceanMarineBase, BaseInsuranceResponse):
    pass