from typing import Optional

from sqlalchemy import Index, String, Text
from sqlalchemy.dialects.postgresql import INET
from sqlalchemy.orm import Mapped, mapped_column

from src.database.base import BaseModel


class WebsiteEnquiry(BaseModel):
    """Platform-level contact/enquiry leads from the marketing site. Not tenant-scoped."""

    __tablename__ = "website_enquiries"

    name: Mapped[str] = mapped_column(String(255), nullable=False)
    email: Mapped[str] = mapped_column(String(255), nullable=False)
    organization: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
    cemetery_type: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
    message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
    status: Mapped[str] = mapped_column(String(50), nullable=False, default="new", server_default="new")
    source: Mapped[str] = mapped_column(String(100), nullable=False, default="marketing", server_default="marketing")
    ip_address: Mapped[Optional[str]] = mapped_column(INET, nullable=True)
    user_agent: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
    # INDL-04: new fields
    type: Mapped[str] = mapped_column(String(20), nullable=False, default="contact", server_default="contact")
    role: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
    phone: Mapped[Optional[str]] = mapped_column(String(50), nullable=True)
    day: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
    time: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)

    __table_args__ = (
        Index("ix_website_enquiries_status", "status"),
        Index("ix_website_enquiries_email", "email"),
        Index("ix_website_enquiries_created_at", "created_at"),
        Index("ix_website_enquiries_type", "type"),
    )
