# FILE: src/apps/public/models/plot_inquiry.py
from __future__ import annotations

from typing import Optional
from uuid import UUID

from sqlalchemy import ForeignKey, String, Text
from sqlalchemy.dialects.postgresql import INET, UUID as PG_UUID
from sqlalchemy.orm import Mapped, mapped_column

from src.database.base import TenantModel


class PlotInquiry(TenantModel):
    __tablename__ = "plot_inquiries"

    # Override tenant_id with explicit FK
    tenant_id: Mapped[UUID] = mapped_column(
        PG_UUID(as_uuid=True),
        ForeignKey("accounts.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )

    plot_id: Mapped[Optional[UUID]] = mapped_column(
        PG_UUID(as_uuid=True),
        ForeignKey("plots.id", ondelete="SET NULL"),
        nullable=True,
    )
    plot_ref: Mapped[str] = mapped_column(String(20), nullable=False)

    # Submitter
    sender_name: Mapped[str] = mapped_column(String(255), nullable=False)
    sender_email: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
    sender_phone: Mapped[Optional[str]] = mapped_column(String(30), nullable=True)
    relationship_: Mapped[str] = mapped_column("relationship", String(100), nullable=False)
    preferred_contact_time: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
    message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)

    # Workflow
    status: Mapped[str] = mapped_column(String(20), nullable=False, default="new")
    # Values: new | read | converted | spam
    reference_id: Mapped[str] = mapped_column(String(30), nullable=False, unique=True, index=True)
    converted_opportunity_id: Mapped[Optional[UUID]] = mapped_column(
        PG_UUID(as_uuid=True),
        ForeignKey("opportunities.id", ondelete="SET NULL"),
        nullable=True,
    )

    # Audit
    ip_address: Mapped[Optional[str]] = mapped_column(INET, nullable=True)
    user_agent: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
