"""messaging initial - email settings, templates, logs, conversations, notifications

Revision ID: messaging_001
Revises: cart_001
Create Date: 2026-02-07
"""
import sqlalchemy as sa

from alembic import op

revision = "messaging_001"
down_revision = "cart_001"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # --- store_email_settings ---
    op.create_table(
        "store_email_settings",
        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, index=True),
        sa.Column("from_email", sa.String(255), nullable=False),
        sa.Column("from_name", sa.String(100), nullable=False),
        sa.Column("reply_to_email", sa.String(255), nullable=True),
        sa.Column("signature_text", sa.Text(), nullable=True),
        sa.Column("signature_html", sa.Text(), nullable=True),
        sa.Column("provider", sa.String(20), nullable=False, server_default="smtp"),
        sa.Column("smtp_host", sa.String(255), nullable=True),
        sa.Column("smtp_port", sa.Integer(), nullable=True, server_default="587"),
        sa.Column("smtp_username", sa.String(255), nullable=True),
        sa.Column("smtp_password", sa.String(500), nullable=True),
        sa.Column("smtp_use_tls", sa.Boolean(), nullable=False, server_default="true"),
        sa.Column("smtp_use_ssl", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("sendgrid_api_key", sa.String(500), nullable=True),
        sa.Column("mailgun_api_key", sa.String(500), nullable=True),
        sa.Column("mailgun_domain", sa.String(255), nullable=True),
        sa.Column("ses_access_key_id", sa.String(100), nullable=True),
        sa.Column("ses_secret_access_key", sa.String(500), nullable=True),
        sa.Column("ses_region", sa.String(50), nullable=True, server_default="eu-west-1"),
        sa.Column("is_configured", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("is_verified", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("last_verified_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("verification_error", 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),
    )
    op.create_index("idx_store_email_settings_configured", "store_email_settings", ["store_id", "is_configured"])

    # --- store_email_templates ---
    op.create_table(
        "store_email_templates",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("template_code", sa.String(100), nullable=False, index=True),
        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="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),
        sa.UniqueConstraint("store_id", "template_code", "language", name="uq_store_email_template_code_language"),
    )

    # --- email_templates (system/platform templates) ---
    op.create_table(
        "email_templates",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("code", sa.String(100), nullable=False, index=True),
        sa.Column("language", sa.String(5), nullable=False, server_default="en"),
        sa.Column("name", sa.String(255), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("category", sa.String(50), nullable=False, server_default="system", index=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("variables", sa.Text(), nullable=True),
        sa.Column("required_variables", sa.Text(), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default="true"),
        sa.Column("is_platform_only", sa.Boolean(), nullable=False, server_default="false"),
        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("ix_email_templates_code_language", "email_templates", ["code", "language"], unique=True)

    # --- email_logs ---
    op.create_table(
        "email_logs",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("template_code", sa.String(100), nullable=True, index=True),
        sa.Column("template_id", sa.Integer(), sa.ForeignKey("email_templates.id"), nullable=True),
        sa.Column("recipient_email", sa.String(255), nullable=False, index=True),
        sa.Column("recipient_name", sa.String(255), nullable=True),
        sa.Column("subject", sa.String(500), nullable=False),
        sa.Column("body_html", sa.Text(), nullable=True),
        sa.Column("body_text", sa.Text(), nullable=True),
        sa.Column("from_email", sa.String(255), nullable=False),
        sa.Column("from_name", sa.String(255), nullable=True),
        sa.Column("reply_to", sa.String(255), nullable=True),
        sa.Column("status", sa.String(20), nullable=False, server_default="pending", index=True),
        sa.Column("sent_at", sa.DateTime(), nullable=True),
        sa.Column("delivered_at", sa.DateTime(), nullable=True),
        sa.Column("opened_at", sa.DateTime(), nullable=True),
        sa.Column("clicked_at", sa.DateTime(), nullable=True),
        sa.Column("error_message", sa.Text(), nullable=True),
        sa.Column("retry_count", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("provider", sa.String(50), nullable=True),
        sa.Column("provider_message_id", sa.String(255), nullable=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=True, index=True),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=True, index=True),
        sa.Column("related_type", sa.String(50), nullable=True),
        sa.Column("related_id", sa.Integer(), nullable=True),
        sa.Column("extra_data", 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),
    )

    # --- conversations ---
    conversation_type_enum = sa.Enum(
        "admin_store", "store_customer", "admin_customer",
        name="conversationtype",
    )
    participant_type_enum = sa.Enum(
        "admin", "store", "customer",
        name="participanttype",
    )
    op.create_table(
        "conversations",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("conversation_type", conversation_type_enum, nullable=False, index=True),
        sa.Column("subject", sa.String(500), nullable=False),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=True, index=True),
        sa.Column("is_closed", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("closed_at", sa.DateTime(), nullable=True),
        sa.Column("closed_by_type", participant_type_enum, nullable=True),
        sa.Column("closed_by_id", sa.Integer(), nullable=True),
        sa.Column("last_message_at", sa.DateTime(), nullable=True, index=True),
        sa.Column("message_count", sa.Integer(), nullable=False, 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("ix_conversations_type_store", "conversations", ["conversation_type", "store_id"])

    # --- conversation_participants ---
    op.create_table(
        "conversation_participants",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("conversation_id", sa.Integer(), sa.ForeignKey("conversations.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("participant_type", sa.Enum("admin", "store", "customer", name="participanttype", create_type=False), nullable=False),
        sa.Column("participant_id", sa.Integer(), nullable=False, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=True),
        sa.Column("unread_count", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("last_read_at", sa.DateTime(), nullable=True),
        sa.Column("email_notifications", sa.Boolean(), nullable=False, server_default="true"),
        sa.Column("muted", sa.Boolean(), nullable=False, server_default="false"),
        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),
        sa.UniqueConstraint("conversation_id", "participant_type", "participant_id", name="uq_conversation_participant"),
    )
    op.create_index("ix_participant_lookup", "conversation_participants", ["participant_type", "participant_id"])

    # --- messages ---
    op.create_table(
        "messages",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("conversation_id", sa.Integer(), sa.ForeignKey("conversations.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("sender_type", sa.Enum("admin", "store", "customer", name="participanttype", create_type=False), nullable=False),
        sa.Column("sender_id", sa.Integer(), nullable=False, index=True),
        sa.Column("content", sa.Text(), nullable=False),
        sa.Column("is_system_message", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("is_deleted", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("deleted_by_type", sa.Enum("admin", "store", "customer", name="participanttype", create_type=False), nullable=True),
        sa.Column("deleted_by_id", sa.Integer(), 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),
    )
    op.create_index("ix_messages_conversation_created", "messages", ["conversation_id", "created_at"])

    # --- message_attachments ---
    op.create_table(
        "message_attachments",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("message_id", sa.Integer(), sa.ForeignKey("messages.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("filename", sa.String(255), nullable=False),
        sa.Column("original_filename", sa.String(255), nullable=False),
        sa.Column("file_path", sa.String(1000), nullable=False),
        sa.Column("file_size", sa.Integer(), nullable=False),
        sa.Column("mime_type", sa.String(100), nullable=False),
        sa.Column("is_image", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("image_width", sa.Integer(), nullable=True),
        sa.Column("image_height", sa.Integer(), nullable=True),
        sa.Column("thumbnail_path", sa.String(1000), 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),
    )

    # --- admin_notifications ---
    op.create_table(
        "admin_notifications",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("type", sa.String(50), nullable=False, index=True),
        sa.Column("priority", sa.String(20), nullable=True, server_default="normal", index=True),
        sa.Column("title", sa.String(200), nullable=False),
        sa.Column("message", sa.Text(), nullable=False),
        sa.Column("is_read", sa.Boolean(), nullable=True, server_default="false", index=True),
        sa.Column("read_at", sa.DateTime(), nullable=True),
        sa.Column("read_by_user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=True),
        sa.Column("action_required", sa.Boolean(), nullable=True, server_default="false", index=True),
        sa.Column("action_url", sa.String(500), nullable=True),
        sa.Column("notification_metadata", sa.JSON(), 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),
    )


def downgrade() -> None:
    op.drop_table("admin_notifications")
    op.drop_table("message_attachments")
    op.drop_table("messages")
    op.drop_table("conversation_participants")
    op.drop_table("conversations")
    op.drop_table("email_logs")
    op.drop_table("email_templates")
    op.drop_table("store_email_templates")
    op.drop_table("store_email_settings")
    sa.Enum(name="conversationtype").drop(op.get_bind(), checkfirst=True)
    sa.Enum(name="participanttype").drop(op.get_bind(), checkfirst=True)
