"""Phase PB5 marketplace lead foundation

Revision ID: 20260525_phase_pb5_marketplace_leads
Revises: 20260524_phase_pb1_platform_billing
Create Date: 2026-05-25
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa

revision = "20260525_phase_pb5_marketplace_leads"
down_revision = "20260524_phase_pb1_platform_billing"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "marketplace_leads",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("lead_no", sa.String(length=50), nullable=False),
        sa.Column("source", sa.String(length=50), nullable=False, server_default="manual"),
        sa.Column("service_category", sa.String(length=100), nullable=True),
        sa.Column("service_requested", sa.String(length=200), nullable=False),
        sa.Column("lead_name", sa.String(length=200), nullable=False),
        sa.Column("business_name", sa.String(length=200), nullable=True),
        sa.Column("email", sa.String(length=255), nullable=True),
        sa.Column("mobile", sa.String(length=20), nullable=True),
        sa.Column("city", sa.String(length=100), nullable=True),
        sa.Column("state", sa.String(length=100), nullable=True),
        sa.Column("message", sa.Text(), nullable=True),
        sa.Column("status", sa.String(length=30), nullable=False, server_default="NEW"),
        sa.Column("priority", sa.String(length=30), nullable=False, server_default="NORMAL"),
        sa.Column("estimated_value", sa.Numeric(12, 2), nullable=False, server_default="0.00"),
        sa.Column("assigned_tenant_id", sa.Integer(), nullable=True),
        sa.Column("assigned_branch_id", sa.Integer(), nullable=True),
        sa.Column("assigned_partner_user_id", sa.Integer(), nullable=True),
        sa.Column("assigned_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("assigned_by_user_id", sa.Integer(), nullable=True),
        sa.Column("converted_client_id", sa.Integer(), nullable=True),
        sa.Column("converted_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("converted_by_user_id", sa.Integer(), nullable=True),
        sa.Column("created_by_user_id", sa.Integer(), nullable=True),
        sa.Column("updated_by_user_id", sa.Integer(), nullable=True),
        sa.Column("created_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(["assigned_tenant_id"], ["tenants.id"]),
        sa.ForeignKeyConstraint(["assigned_branch_id"], ["branches.id"]),
        sa.ForeignKeyConstraint(["assigned_partner_user_id"], ["users.id"]),
        sa.ForeignKeyConstraint(["assigned_by_user_id"], ["users.id"]),
        sa.ForeignKeyConstraint(["converted_client_id"], ["clients.id"]),
        sa.ForeignKeyConstraint(["converted_by_user_id"], ["users.id"]),
        sa.ForeignKeyConstraint(["created_by_user_id"], ["users.id"]),
        sa.ForeignKeyConstraint(["updated_by_user_id"], ["users.id"]),
        sa.UniqueConstraint("lead_no", name="uq_marketplace_leads_lead_no"),
    )
    for col in ["lead_no", "source", "service_category", "service_requested", "lead_name", "business_name", "email", "mobile", "status", "priority", "assigned_tenant_id", "assigned_branch_id", "assigned_partner_user_id", "converted_client_id"]:
        op.create_index(f"ix_marketplace_leads_{col}", "marketplace_leads", [col])

    op.create_table(
        "marketplace_lead_assignments",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("lead_id", sa.Integer(), nullable=False),
        sa.Column("tenant_id", sa.Integer(), nullable=False),
        sa.Column("branch_id", sa.Integer(), nullable=True),
        sa.Column("partner_user_id", sa.Integer(), nullable=True),
        sa.Column("status", sa.String(length=30), nullable=False, server_default="ASSIGNED"),
        sa.Column("notes", sa.Text(), nullable=True),
        sa.Column("assigned_by_user_id", sa.Integer(), nullable=True),
        sa.Column("assigned_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(["lead_id"], ["marketplace_leads.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["tenant_id"], ["tenants.id"]),
        sa.ForeignKeyConstraint(["branch_id"], ["branches.id"]),
        sa.ForeignKeyConstraint(["partner_user_id"], ["users.id"]),
        sa.ForeignKeyConstraint(["assigned_by_user_id"], ["users.id"]),
    )
    for col in ["lead_id", "tenant_id", "branch_id", "partner_user_id", "status"]:
        op.create_index(f"ix_marketplace_lead_assignments_{col}", "marketplace_lead_assignments", [col])


def downgrade() -> None:
    op.drop_table("marketplace_lead_assignments")
    op.drop_table("marketplace_leads")
