import uuid
from sqlmodel import SQLModel, Field, Column, text
from datetime import datetime
from typing import Optional
from sqlalchemy import DateTime
from sqlalchemy.sql import func


class HubspotDealData(SQLModel, table=True):
    __tablename__ = "hubspot_deal_data"

    id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)

    # HubSpot Deal identifiers
    deal_id: str = Field(index=True, nullable=False)
    dealname: Optional[str] = Field(default=None)
    dealstage: Optional[str] = Field(default=None)
    company_id: Optional[str] = Field(default=None)
    project_code: Optional[str] = Field(default=None)

    # HubSpot custom properties
    landlord_property_id: Optional[str] = Field(default=None)
    uprn: Optional[str] = Field(default=None)
    outcome: Optional[str] = Field(default=None)
    outcome_notes: Optional[str] = Field(default=None)

    major_condition_issue_description: Optional[str] = Field(default=None)
    major_condition_issue_photos: Optional[str] = Field(default=None)
    major_condition_issue_evidence_s3_url: Optional[str] = Field(default=None)

    coordination_status: Optional[str] = Field(default=None)
    coordination_comments: Optional[str] = Field(default=None)
    design_status: Optional[str] = Field(default=None)

    listing_id: Optional[str] = Field(default=None)
    pashub_link: Optional[str] = Field(default=None)
    sharepoint_link: Optional[str] = Field(default=None)
    dampmould_growth: Optional[str] = Field(default=None)
    damp_mould_and_repairs_comments: Optional[str] = Field(default=None)
    pre_sap: Optional[str] = Field(default=None)
    batch: Optional[str] = Field(default=None)
    block_reference: Optional[str] = Field(default=None)
    epc_prn: Optional[str] = Field(default=None)
    potential_post_sap_score_dropdown: Optional[str] = Field(default=None)
    ei_score: Optional[str] = Field(default=None)
    ei_score__potential_: Optional[str] = Field(default=None)
    epc_sap_score: Optional[str] = Field(default=None)
    epc_sap_score__potential_: Optional[str] = Field(default=None)
    coordinator: Optional[str] = Field(default=None)
    mtp_completion_date: Optional[datetime] = Field(default=None)
    mtp_re_model_completion_date: Optional[datetime] = Field(default=None)
    ioe_v3_completion_date: Optional[datetime] = Field(default=None)
    proposed_measures: Optional[str] = Field(default=None)
    approved_package: Optional[str] = Field(default=None)
    designer: Optional[str] = Field(default=None)
    design_completion_date: Optional[datetime] = Field(default=None)
    actual_measures_installed: Optional[str] = Field(default=None)
    installer: Optional[str] = Field(default=None)
    installer_handover: Optional[str] = Field(default=None)
    lodgement_status: Optional[str] = Field(default=None)
    measures_lodgement_date: Optional[datetime] = Field(default=None)
    lodgement_date: Optional[datetime] = Field(default=None)
    expected_commencement_date: Optional[datetime] = Field(default=None)
    surveyor: Optional[str] = Field(default=None)
    confirmed_survey_date: Optional[datetime] = Field(default=None)
    confirmed_survey_time: Optional[str] = Field(default=None)
    surveyed_date: Optional[datetime] = Field(default=None)
    design_type: Optional[str] = Field(default=None)

    survey_type: Optional[str] = Field(default=None)
    measures_for_pibi_ordered: Optional[str] = Field(default=None)
    pibi_order_date: Optional[datetime] = Field(default=None)
    pibi_completed_date: Optional[datetime] = Field(default=None)
    property_halted_date: Optional[datetime] = Field(default=None)
    property_halted_reason: Optional[str] = Field(default=None)
    technical_approved_measures_for_install: Optional[str] = Field(default=None)
    sent_to_installer_for_pricing: Optional[datetime] = Field(default=None)
    domna_survey_required: Optional[bool] = Field(default=None)
    domna_survey_date: Optional[datetime] = Field(default=None)

    created_at: Optional[datetime] = Field(
        sa_column=Column(
            DateTime(timezone=True),
            server_default=text("(NOW() AT TIME ZONE 'utc')"),
            nullable=False,
        ),
        default=func.now(),
    )  # Nullable in db but optional here as value is set on db save for new record

    updated_at: Optional[datetime] = Field(
        sa_column=Column(
            DateTime(timezone=True),
            server_default=text("(NOW() AT TIME ZONE 'utc')"),
            onupdate=func.now(),
            nullable=False,
        ),
        default=func.now(),
    )  # Nullable in db but optional here as value is set on db save for new record
