"""Add distribution_schedules table

Revision ID: a7b9c1d3ef67
Revises: f6g8h0i2de56
Create Date: 2026-05-23 15:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

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


def upgrade() -> None:
    """Add distribution_schedules table."""
    op.create_table(
        'distribution_schedules',
        sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('gen_random_uuid()'), nullable=False),
        sa.Column('organization_id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('content_title', sa.String(500), nullable=False),
        sa.Column('content_id', postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column('platforms', postgresql.JSONB(), nullable=True),
        sa.Column('tips', postgresql.JSONB(), nullable=True),
        sa.Column('status', sa.String(20), server_default='pending', nullable=False),
        sa.Column('created_by', postgresql.UUID(as_uuid=True), 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), server_default=sa.text('NOW()'), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        sa.ForeignKeyConstraint(['organization_id'], ['organizations.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['content_id'], ['contents.id'], ondelete='SET NULL'),
        sa.ForeignKeyConstraint(['created_by'], ['users.id'], ondelete='SET NULL')
    )
    op.create_index('idx_distribution_schedules_organization_id', 'distribution_schedules', ['organization_id'])
    op.create_index('idx_distribution_schedules_status', 'distribution_schedules', ['status'])
    op.create_index('idx_distribution_schedules_created_by', 'distribution_schedules', ['created_by'])


def downgrade() -> None:
    """Remove distribution_schedules table."""
    op.drop_index('idx_distribution_schedules_created_by', table_name='distribution_schedules')
    op.drop_index('idx_distribution_schedules_status', table_name='distribution_schedules')
    op.drop_index('idx_distribution_schedules_organization_id', table_name='distribution_schedules')
    op.drop_table('distribution_schedules')
