"""
Contact catalog: people/offices that get notified when an alert is reported.

A Contact has a role (PORT_AUTHORITY or OWNER). When the user clicks REPORT
on an alert, the backend resolves recipients by matching:
   - port_name == aid.puerto_responsable      → port authorities
   - company_name == aid.empresa_responsable  → owners
"""
from sqlalchemy import Column, String, DateTime, Text
from sqlalchemy.sql import func
from database import Base


class Contact(Base):
    __tablename__ = "contacts"

    id                = Column(String, primary_key=True)
    name              = Column(String, nullable=False)
    role              = Column(String, nullable=False)   # PORT_AUTHORITY | OWNER
    email             = Column(String, nullable=True)
    phone             = Column(String, nullable=True)
    whatsapp          = Column(String, nullable=True)    # E.164 e.g. +573001234567
    port_name         = Column(String, nullable=True)    # for PORT_AUTHORITY
    company_name      = Column(String, nullable=True)    # for OWNER
    preferred_channel = Column(String, default="EMAIL")  # EMAIL | SMS | WHATSAPP
    notes             = Column(Text, nullable=True)
    creado_en         = Column(DateTime, server_default=func.now())
    modificado_en     = Column(DateTime, onupdate=func.now())


class AlertReport(Base):
    """Audit log: each REPORT click creates one row per channel/recipient."""
    __tablename__ = "alert_reports"

    id              = Column(String, primary_key=True)
    alert_id        = Column(String, nullable=True, index=True)  # client-generated UUID
    alert_tipo      = Column(String, nullable=True)
    alert_subtipo   = Column(String, nullable=True)
    aid_id          = Column(String, nullable=True)
    aid_nombre      = Column(String, nullable=True)
    mmsi            = Column(String, nullable=True)
    contact_id      = Column(String, nullable=True)
    contact_name    = Column(String, nullable=True)
    channel         = Column(String, nullable=False)   # EMAIL | SMS | WHATSAPP
    recipient       = Column(String, nullable=False)   # the actual address used
    message         = Column(Text, nullable=True)
    reported_by     = Column(String, nullable=False)   # username
    reported_at     = Column(DateTime, server_default=func.now())
