"""add llm artifacts and yield landing config

Revision ID: 016_add_llm_artifacts_and_yield_landing_config
Revises: 015_add_subscription_referral_bonus_domains
Create Date: 2025-12-17
"""

from __future__ import annotations

import sqlalchemy as sa
from alembic import op


revision = "016_add_llm_artifacts_and_yield_landing_config"
down_revision = "015_add_subscription_referral_bonus_domains"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "llm_artifacts",
        sa.Column("id", sa.Integer(), primary_key=True, nullable=False),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=True),
        sa.Column("kind", sa.String(length=50), nullable=False),
        sa.Column("domain", sa.String(length=255), nullable=False),
        sa.Column("prompt_version", sa.String(length=50), nullable=False),
        sa.Column("model", sa.String(length=100), nullable=False),
        sa.Column("payload_json", sa.Text(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.Column("expires_at", sa.DateTime(), nullable=True),
    )
    op.create_index("ix_llm_artifacts_id", "llm_artifacts", ["id"])
    op.create_index("ix_llm_artifacts_user_id", "llm_artifacts", ["user_id"])
    op.create_index("ix_llm_artifacts_kind", "llm_artifacts", ["kind"])
    op.create_index("ix_llm_artifacts_domain", "llm_artifacts", ["domain"])
    op.create_index("ix_llm_artifacts_prompt_version", "llm_artifacts", ["prompt_version"])
    op.create_index("ix_llm_artifacts_created_at", "llm_artifacts", ["created_at"])
    op.create_index("ix_llm_artifacts_expires_at", "llm_artifacts", ["expires_at"])
    op.create_index(
        "ix_llm_artifacts_kind_domain_prompt",
        "llm_artifacts",
        ["kind", "domain", "prompt_version"],
    )

    # Yield landing config (generated by LLM on activation)
    op.add_column("yield_domains", sa.Column("landing_config_json", sa.Text(), nullable=True))
    op.add_column("yield_domains", sa.Column("landing_template", sa.String(length=50), nullable=True))
    op.add_column("yield_domains", sa.Column("landing_headline", sa.String(length=300), nullable=True))
    op.add_column("yield_domains", sa.Column("landing_intro", sa.Text(), nullable=True))
    op.add_column("yield_domains", sa.Column("landing_cta_label", sa.String(length=120), nullable=True))
    op.add_column("yield_domains", sa.Column("landing_model", sa.String(length=100), nullable=True))
    op.add_column("yield_domains", sa.Column("landing_generated_at", sa.DateTime(), nullable=True))


def downgrade() -> None:
    op.drop_column("yield_domains", "landing_generated_at")
    op.drop_column("yield_domains", "landing_model")
    op.drop_column("yield_domains", "landing_cta_label")
    op.drop_column("yield_domains", "landing_intro")
    op.drop_column("yield_domains", "landing_headline")
    op.drop_column("yield_domains", "landing_template")
    op.drop_column("yield_domains", "landing_config_json")

    op.drop_index("ix_llm_artifacts_kind_domain_prompt", table_name="llm_artifacts")
    op.drop_index("ix_llm_artifacts_expires_at", table_name="llm_artifacts")
    op.drop_index("ix_llm_artifacts_created_at", table_name="llm_artifacts")
    op.drop_index("ix_llm_artifacts_prompt_version", table_name="llm_artifacts")
    op.drop_index("ix_llm_artifacts_domain", table_name="llm_artifacts")
    op.drop_index("ix_llm_artifacts_kind", table_name="llm_artifacts")
    op.drop_index("ix_llm_artifacts_user_id", table_name="llm_artifacts")
    op.drop_index("ix_llm_artifacts_id", table_name="llm_artifacts")
    op.drop_table("llm_artifacts")

