"""add checkouts module

Creates all tables for the Checkouts module (PRD-HW-CHECKOUTS):
  - checkouts
  - checkout_links
  - checkout_line_items
  - checkout_activities
  - checkout_settings
Adds checkout_id FK column to payment_requests.

Revision ID: a1b2c3d4e5f9
Revises: 497fbd5f86aa
Create Date: 2026-03-23 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'a1b2c3d4e5f9'
down_revision: Union[str, Sequence[str], None] = '497fbd5f86aa'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ------------------------------------------------------------------
    # checkouts
    # ------------------------------------------------------------------
    op.create_table(
        'checkouts',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('checkout_literal', sa.String(length=50), nullable=True),
        sa.Column('merchant_id', sa.Integer(), nullable=False),
        sa.Column('title', sa.String(length=255), nullable=False),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('checkout_type', sa.String(length=30), nullable=False, server_default='merchant_defined'),
        sa.Column('status', sa.String(length=20), nullable=False, server_default='draft'),
        sa.Column('amount', sa.Float(), nullable=True),
        sa.Column('currency', sa.String(length=10), nullable=False, server_default='USD'),
        sa.Column('min_amount', sa.Float(), nullable=True),
        sa.Column('max_amount', sa.Float(), nullable=True),
        sa.Column('suggested_amounts', sa.JSON(), nullable=True),
        sa.Column('default_amount', sa.Float(), nullable=True),
        sa.Column('tip_enabled', sa.Boolean(), nullable=False, server_default=sa.text('false')),
        sa.Column('tip_type', sa.String(length=20), nullable=True, server_default='percentage'),
        sa.Column('tip_percentages', sa.JSON(), nullable=True),
        sa.Column('payment_frequency', sa.String(length=50), nullable=True),
        sa.Column('authorization_type', sa.String(length=50), nullable=True),
        sa.Column('split_config', sa.JSON(), nullable=True),
        sa.Column('recurring_config', sa.JSON(), nullable=True),
        sa.Column('require_payer_verification', sa.Boolean(), nullable=False, server_default=sa.text('false')),
        sa.Column('payer_verification_type', sa.String(length=20), nullable=True, server_default='email'),
        sa.Column('expires_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('allow_link_regeneration', sa.Boolean(), nullable=False, server_default=sa.text('true')),
        sa.Column('redirect_url', sa.Text(), nullable=True),
        sa.Column('thank_you_message', sa.Text(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(['merchant_id'], ['merchants.id'], name='fk_checkouts_merchant_id'),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_index('ix_checkouts_id', 'checkouts', ['id'], unique=False)
    op.create_index('ix_checkouts_checkout_literal', 'checkouts', ['checkout_literal'], unique=True)
    op.create_index('ix_checkouts_merchant_id', 'checkouts', ['merchant_id'], unique=False)
    op.create_index('ix_checkouts_status', 'checkouts', ['status'], unique=False)

    # ------------------------------------------------------------------
    # checkout_links
    # ------------------------------------------------------------------
    op.create_table(
        'checkout_links',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('checkout_id', sa.Integer(), nullable=False),
        sa.Column('token', sa.String(length=128), nullable=False),
        sa.Column('status', sa.String(length=20), nullable=False, server_default='active'),
        sa.Column('click_count', sa.Integer(), nullable=False, server_default=sa.text('0')),
        sa.Column('last_clicked_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
        sa.Column('revoked_at', sa.DateTime(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(['checkout_id'], ['checkouts.id'], name='fk_checkout_links_checkout_id'),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_index('ix_checkout_links_id', 'checkout_links', ['id'], unique=False)
    op.create_index('ix_checkout_links_token', 'checkout_links', ['token'], unique=True)
    op.create_index('ix_checkout_links_checkout_id', 'checkout_links', ['checkout_id'], unique=False)
    op.create_index('ix_checkout_links_status', 'checkout_links', ['status'], unique=False)

    # ------------------------------------------------------------------
    # checkout_line_items
    # ------------------------------------------------------------------
    op.create_table(
        'checkout_line_items',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('checkout_id', sa.Integer(), nullable=False),
        sa.Column('product_id', sa.Integer(), nullable=True),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('quantity', sa.Integer(), nullable=False, server_default=sa.text('1')),
        sa.Column('unit_price', sa.Float(), nullable=False, server_default=sa.text('0')),
        sa.Column('discount_amount', sa.Float(), nullable=True, server_default=sa.text('0')),
        sa.ForeignKeyConstraint(['checkout_id'], ['checkouts.id'], name='fk_checkout_line_items_checkout_id'),
        sa.ForeignKeyConstraint(['product_id'], ['products.id'], name='fk_checkout_line_items_product_id'),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_index('ix_checkout_line_items_id', 'checkout_line_items', ['id'], unique=False)
    op.create_index('ix_checkout_line_items_checkout_id', 'checkout_line_items', ['checkout_id'], unique=False)
    op.create_index('ix_checkout_line_items_product_id', 'checkout_line_items', ['product_id'], unique=False)

    # ------------------------------------------------------------------
    # checkout_activities
    # ------------------------------------------------------------------
    op.create_table(
        'checkout_activities',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('checkout_id', sa.Integer(), nullable=False),
        sa.Column('merchant_id', sa.Integer(), nullable=False),
        sa.Column('actor_user_id', sa.Integer(), nullable=True),
        sa.Column('actor_customer_id', sa.Integer(), nullable=True),
        sa.Column('event_type', sa.String(length=64), nullable=False),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('metadata', sa.JSON(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
        sa.ForeignKeyConstraint(['checkout_id'], ['checkouts.id'], name='fk_checkout_activities_checkout_id'),
        sa.ForeignKeyConstraint(['merchant_id'], ['merchants.id'], name='fk_checkout_activities_merchant_id'),
        sa.ForeignKeyConstraint(['actor_user_id'], ['users.id'], name='fk_checkout_activities_actor_user_id'),
        sa.ForeignKeyConstraint(['actor_customer_id'], ['customers.id'], name='fk_checkout_activities_actor_customer_id'),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_index('ix_checkout_activities_id', 'checkout_activities', ['id'], unique=False)
    op.create_index('ix_checkout_activities_checkout_id', 'checkout_activities', ['checkout_id'], unique=False)
    op.create_index('ix_checkout_activities_merchant_id', 'checkout_activities', ['merchant_id'], unique=False)
    op.create_index('ix_checkout_activities_event_type', 'checkout_activities', ['event_type'], unique=False)

    # ------------------------------------------------------------------
    # checkout_settings
    # ------------------------------------------------------------------
    op.create_table(
        'checkout_settings',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('merchant_id', sa.Integer(), nullable=False),
        sa.Column('default_tip_enabled', sa.Boolean(), nullable=False, server_default=sa.text('false')),
        sa.Column('default_tip_percentages', sa.JSON(), nullable=True),
        sa.Column('default_thank_you_message', sa.Text(), nullable=True),
        sa.Column('default_redirect_url', sa.Text(), nullable=True),
        sa.Column('default_require_payer_verification', sa.Boolean(), nullable=False, server_default=sa.text('false')),
        sa.Column('default_payer_verification_type', sa.Text(), nullable=True, server_default='email'),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(['merchant_id'], ['merchants.id'], name='fk_checkout_settings_merchant_id'),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('merchant_id', name='uq_checkout_settings_merchant_id'),
    )
    op.create_index('ix_checkout_settings_id', 'checkout_settings', ['id'], unique=False)
    op.create_index('ix_checkout_settings_merchant_id', 'checkout_settings', ['merchant_id'], unique=True)

    # ------------------------------------------------------------------
    # payment_requests — add checkout_id FK
    # ------------------------------------------------------------------
    op.add_column(
        'payment_requests',
        sa.Column('checkout_id', sa.Integer(), nullable=True),
    )
    op.create_index('ix_payment_requests_checkout_id', 'payment_requests', ['checkout_id'], unique=False)
    op.create_foreign_key(
        'fk_payment_requests_checkout_id',
        'payment_requests', 'checkouts',
        ['checkout_id'], ['id'],
    )


def downgrade() -> None:
    # Remove checkout_id from payment_requests
    op.drop_constraint('fk_payment_requests_checkout_id', 'payment_requests', type_='foreignkey')
    op.drop_index('ix_payment_requests_checkout_id', table_name='payment_requests')
    op.drop_column('payment_requests', 'checkout_id')

    # Drop checkout_settings
    op.drop_index('ix_checkout_settings_merchant_id', table_name='checkout_settings')
    op.drop_index('ix_checkout_settings_id', table_name='checkout_settings')
    op.drop_table('checkout_settings')

    # Drop checkout_activities
    op.drop_index('ix_checkout_activities_event_type', table_name='checkout_activities')
    op.drop_index('ix_checkout_activities_merchant_id', table_name='checkout_activities')
    op.drop_index('ix_checkout_activities_checkout_id', table_name='checkout_activities')
    op.drop_index('ix_checkout_activities_id', table_name='checkout_activities')
    op.drop_table('checkout_activities')

    # Drop checkout_line_items
    op.drop_index('ix_checkout_line_items_product_id', table_name='checkout_line_items')
    op.drop_index('ix_checkout_line_items_checkout_id', table_name='checkout_line_items')
    op.drop_index('ix_checkout_line_items_id', table_name='checkout_line_items')
    op.drop_table('checkout_line_items')

    # Drop checkout_links
    op.drop_index('ix_checkout_links_status', table_name='checkout_links')
    op.drop_index('ix_checkout_links_checkout_id', table_name='checkout_links')
    op.drop_index('ix_checkout_links_token', table_name='checkout_links')
    op.drop_index('ix_checkout_links_id', table_name='checkout_links')
    op.drop_table('checkout_links')

    # Drop checkouts
    op.drop_index('ix_checkouts_status', table_name='checkouts')
    op.drop_index('ix_checkouts_merchant_id', table_name='checkouts')
    op.drop_index('ix_checkouts_checkout_literal', table_name='checkouts')
    op.drop_index('ix_checkouts_id', table_name='checkouts')
    op.drop_table('checkouts')
