"""cms initial - content pages, store themes, media files

Revision ID: cms_001
Revises: marketplace_001
Create Date: 2026-02-07
"""
import sqlalchemy as sa

from alembic import op

revision = "cms_001"
down_revision = "marketplace_001"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # --- content_pages ---
    op.create_table(
        "content_pages",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("platform_id", sa.Integer(), sa.ForeignKey("platforms.id", ondelete="CASCADE"), nullable=False, index=True, comment="Platform this page belongs to"),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id", ondelete="CASCADE"), nullable=True, index=True, comment="Store this page belongs to (NULL for platform/default pages)"),
        sa.Column("is_platform_page", sa.Boolean(), nullable=False, server_default="false", comment="True = platform marketing page (homepage, pricing); False = store default or override"),
        sa.Column("slug", sa.String(100), nullable=False, index=True),
        sa.Column("title", sa.String(200), nullable=False),
        sa.Column("content", sa.Text(), nullable=False),
        sa.Column("content_format", sa.String(20), nullable=True, server_default="html"),
        sa.Column("template", sa.String(50), nullable=False, server_default="default"),
        sa.Column("sections", sa.JSON(), nullable=True, comment="Structured homepage sections (hero, features, pricing, cta) with i18n"),
        sa.Column("meta_description", sa.String(300), nullable=True),
        sa.Column("meta_keywords", sa.String(300), nullable=True),
        sa.Column("is_published", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("published_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("display_order", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("show_in_footer", sa.Boolean(), nullable=False, server_default="true"),
        sa.Column("show_in_header", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("show_in_legal", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("created_by", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
        sa.Column("updated_by", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
        sa.UniqueConstraint("platform_id", "store_id", "slug", name="uq_platform_store_slug"),
    )
    op.create_index("idx_platform_store_published", "content_pages", ["platform_id", "store_id", "is_published"])
    op.create_index("idx_platform_slug_published", "content_pages", ["platform_id", "slug", "is_published"])
    op.create_index("idx_platform_page_type", "content_pages", ["platform_id", "is_platform_page"])

    # --- store_themes ---
    op.create_table(
        "store_themes",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id", ondelete="CASCADE"), unique=True, nullable=False),
        sa.Column("theme_name", sa.String(100), nullable=True, server_default="default"),
        sa.Column("is_active", sa.Boolean(), nullable=True, server_default="true"),
        sa.Column("colors", sa.JSON(), nullable=True),
        sa.Column("font_family_heading", sa.String(100), nullable=True, server_default="Inter, sans-serif"),
        sa.Column("font_family_body", sa.String(100), nullable=True, server_default="Inter, sans-serif"),
        sa.Column("logo_url", sa.String(500), nullable=True),
        sa.Column("logo_dark_url", sa.String(500), nullable=True),
        sa.Column("favicon_url", sa.String(500), nullable=True),
        sa.Column("banner_url", sa.String(500), nullable=True),
        sa.Column("layout_style", sa.String(50), nullable=True, server_default="grid"),
        sa.Column("header_style", sa.String(50), nullable=True, server_default="fixed"),
        sa.Column("product_card_style", sa.String(50), nullable=True, server_default="modern"),
        sa.Column("custom_css", sa.Text(), nullable=True),
        sa.Column("social_links", sa.JSON(), nullable=True),
        sa.Column("meta_title_template", sa.String(200), nullable=True),
        sa.Column("meta_description", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
    )

    # --- media_files ---
    op.create_table(
        "media_files",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=False),
        sa.Column("filename", sa.String(255), nullable=False),
        sa.Column("original_filename", sa.String(255), nullable=True),
        sa.Column("file_path", sa.String(500), nullable=False),
        sa.Column("media_type", sa.String(20), nullable=False),
        sa.Column("mime_type", sa.String(100), nullable=True),
        sa.Column("file_size", sa.Integer(), nullable=True),
        sa.Column("width", sa.Integer(), nullable=True),
        sa.Column("height", sa.Integer(), nullable=True),
        sa.Column("thumbnail_path", sa.String(500), nullable=True),
        sa.Column("alt_text", sa.String(500), nullable=True),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("folder", sa.String(100), nullable=True, server_default="general"),
        sa.Column("tags", sa.JSON(), nullable=True),
        sa.Column("extra_metadata", sa.JSON(), nullable=True),
        sa.Column("is_optimized", sa.Boolean(), nullable=True, server_default="false"),
        sa.Column("optimized_size", sa.Integer(), nullable=True),
        sa.Column("usage_count", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
    )
    op.create_index("idx_media_store_id", "media_files", ["store_id"])
    op.create_index("idx_media_store_folder", "media_files", ["store_id", "folder"])
    op.create_index("idx_media_store_type", "media_files", ["store_id", "media_type"])
    op.create_index("idx_media_filename", "media_files", ["filename"])


def downgrade() -> None:
    op.drop_table("media_files")
    op.drop_table("store_themes")
    op.drop_table("content_pages")
