# alembic/versions/u9c0d1e2f3g4_add_vendor_email_templates.py
"""Add vendor email templates and enhance email_templates table.

Revision ID: u9c0d1e2f3g4
Revises: t8b9c0d1e2f3
Create Date: 2026-01-03

Changes:
- Add is_platform_only column to email_templates (templates that vendors cannot override)
- Add required_variables column to email_templates (JSON list of required variables)
- Create vendor_email_templates table for vendor-specific template overrides
"""

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "u9c0d1e2f3g4"
down_revision = "t8b9c0d1e2f3"
branch_labels = None
depends_on = None


def upgrade():
    # Add new columns to email_templates
    op.add_column(
        "email_templates",
        sa.Column("is_platform_only", sa.Boolean(), nullable=False, server_default="0"),
    )
    op.add_column(
        "email_templates",
        sa.Column("required_variables", sa.Text(), nullable=True),
    )

    # Create vendor_email_templates table
    op.create_table(
        "vendor_email_templates",
        sa.Column("id", sa.Integer(), nullable=False, autoincrement=True),
        sa.Column("vendor_id", sa.Integer(), nullable=False),
        sa.Column("template_code", sa.String(100), nullable=False),
        sa.Column("language", sa.String(5), nullable=False, server_default="en"),
        sa.Column("name", sa.String(255), nullable=True),
        sa.Column("subject", sa.String(500), nullable=False),
        sa.Column("body_html", sa.Text(), nullable=False),
        sa.Column("body_text", sa.Text(), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default="1"),
        sa.Column(
            "created_at",
            sa.DateTime(),
            nullable=False,
            server_default=sa.text("CURRENT_TIMESTAMP"),
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(),
            nullable=False,
            server_default=sa.text("CURRENT_TIMESTAMP"),
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.ForeignKeyConstraint(
            ["vendor_id"],
            ["vendors.id"],
            name="fk_vendor_email_templates_vendor_id",
            ondelete="CASCADE",
        ),
        sa.UniqueConstraint(
            "vendor_id",
            "template_code",
            "language",
            name="uq_vendor_email_template_code_language",
        ),
    )

    # Create indexes for performance
    op.create_index(
        "ix_vendor_email_templates_vendor_id",
        "vendor_email_templates",
        ["vendor_id"],
    )
    op.create_index(
        "ix_vendor_email_templates_template_code",
        "vendor_email_templates",
        ["template_code"],
    )
    op.create_index(
        "ix_vendor_email_templates_lookup",
        "vendor_email_templates",
        ["vendor_id", "template_code", "language"],
    )

    # Add unique constraint to email_templates for code+language
    # This ensures we can reliably look up platform templates
    op.create_index(
        "ix_email_templates_code_language",
        "email_templates",
        ["code", "language"],
        unique=True,
    )


def downgrade():
    # Drop indexes
    op.drop_index("ix_email_templates_code_language", table_name="email_templates")
    op.drop_index("ix_vendor_email_templates_lookup", table_name="vendor_email_templates")
    op.drop_index("ix_vendor_email_templates_template_code", table_name="vendor_email_templates")
    op.drop_index("ix_vendor_email_templates_vendor_id", table_name="vendor_email_templates")

    # Drop vendor_email_templates table
    op.drop_table("vendor_email_templates")

    # Remove new columns from email_templates
    op.drop_column("email_templates", "required_variables")
    op.drop_column("email_templates", "is_platform_only")
