"""Marketing and integration aggregates — Phase 14.7."""
from __future__ import annotations

import uuid

from sqlalchemy import Index, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import JSON

from app.core.database import Base
from app.models.base import (
    ActorAuditMixin,
    SoftDeleteMixin,
    TenantMixin,
    TimestampMixin,
    UUIDPrimaryKeyMixin,
)
from app.models.types import GUID, IntegrationDispatchStatus, LifecycleStatus


class MarketingCampaignRef(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "marketing_campaign_refs"

    organization_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    branch_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    code: Mapped[str] = mapped_column(String(50), nullable=False)
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    external_campaign_ref: Mapped[str | None] = mapped_column(String(255), nullable=True)
    channel: Mapped[str | None] = mapped_column(String(50), nullable=True)
    status: Mapped[LifecycleStatus] = mapped_column(
        default=LifecycleStatus.DRAFT, nullable=False
    )
    config: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    __table_args__ = (
        Index(
            "ix_marketing_campaign_refs_tenant_code",
            "tenant_id",
            "code",
            unique=True,
        ),
        Index("ix_marketing_campaign_refs_org", "tenant_id", "organization_id"),
    )


class LoyaltyIntegrationConfig(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "loyalty_integration_configs"

    organization_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    code: Mapped[str] = mapped_column(String(50), nullable=False)
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    external_program_ref: Mapped[str | None] = mapped_column(String(255), nullable=True)
    earn_rules: Mapped[dict | None] = mapped_column(JSON, nullable=True)
    status: Mapped[LifecycleStatus] = mapped_column(
        default=LifecycleStatus.ACTIVE, nullable=False
    )

    __table_args__ = (
        Index(
            "ix_loyalty_integration_configs_tenant_code",
            "tenant_id",
            "code",
            unique=True,
        ),
    )


class CommunicationIntegrationConfig(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "communication_integration_configs"

    organization_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    code: Mapped[str] = mapped_column(String(50), nullable=False)
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    template_mappings: Mapped[dict | None] = mapped_column(JSON, nullable=True)
    status: Mapped[LifecycleStatus] = mapped_column(
        default=LifecycleStatus.ACTIVE, nullable=False
    )

    __table_args__ = (
        Index(
            "ix_communication_integration_configs_tenant_code",
            "tenant_id",
            "code",
            unique=True,
        ),
    )


class IntegrationDispatch(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "integration_dispatches"

    organization_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    integration_kind: Mapped[str] = mapped_column(String(50), nullable=False)
    config_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    event_type: Mapped[str] = mapped_column(String(100), nullable=False)
    payload_ref: Mapped[str | None] = mapped_column(String(255), nullable=True)
    status: Mapped[IntegrationDispatchStatus] = mapped_column(
        default=IntegrationDispatchStatus.PENDING, nullable=False
    )
    response_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)

    __table_args__ = (
        Index("ix_integration_dispatches_kind", "tenant_id", "integration_kind"),
        Index("ix_integration_dispatches_status", "tenant_id", "status"),
    )
