"""add Payments & Invoices to Entitlements Domain

Revision ID: 26e20f27d399
Revises: fa6113ce4306
Create Date: 2025-05-13 15:54:51.069984

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

# revision identifiers, used by Alembic.
revision = '26e20f27d399'
down_revision = 'fa6113ce4306'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('license_period',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('license_id', sa.Integer(), nullable=False),
    sa.Column('period_number', sa.Integer(), nullable=False),
    sa.Column('period_start', sa.Date(), nullable=False),
    sa.Column('period_end', sa.Date(), nullable=False),
    sa.Column('status', sa.Enum('UPCOMING', 'PENDING', 'ACTIVE', 'COMPLETED', 'INVOICED', 'CLOSED', name='periodstatus'), nullable=False),
    sa.Column('upcoming_at', sa.DateTime(), nullable=True),
    sa.Column('pending_at', sa.DateTime(), nullable=True),
    sa.Column('active_at', sa.DateTime(), nullable=True),
    sa.Column('completed_at', sa.DateTime(), nullable=True),
    sa.Column('invoiced_at', sa.DateTime(), nullable=True),
    sa.Column('closed_at', sa.DateTime(), nullable=True),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
    sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
    sa.Column('created_by', sa.Integer(), nullable=True),
    sa.Column('updated_by', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['created_by'], ['public.user.id'], ),
    sa.ForeignKeyConstraint(['license_id'], ['public.license.id'], ),
    sa.ForeignKeyConstraint(['updated_by'], ['public.user.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='public'
    )
    op.create_table('payment',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('license_period_id', sa.Integer(), nullable=True),
    sa.Column('payment_type', sa.Enum('PREPAID', 'POSTPAID', name='paymenttype'), nullable=False),
    sa.Column('amount', sa.Numeric(precision=10, scale=2), nullable=False),
    sa.Column('currency', sa.String(length=3), nullable=False),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('status', sa.Enum('PENDING', 'PAID', 'FAILED', 'CANCELLED', name='paymentstatus'), nullable=False),
    sa.Column('external_payment_id', sa.String(length=255), nullable=True),
    sa.Column('payment_method', sa.String(length=50), nullable=True),
    sa.Column('provider_data', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.Column('paid_at', sa.DateTime(), nullable=True),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
    sa.Column('created_by', sa.Integer(), nullable=True),
    sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
    sa.Column('updated_by', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['created_by'], ['public.user.id'], ),
    sa.ForeignKeyConstraint(['license_period_id'], ['public.license_period.id'], ),
    sa.ForeignKeyConstraint(['updated_by'], ['public.user.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='public'
    )
    op.create_table('invoice',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('license_period_id', sa.Integer(), nullable=False),
    sa.Column('payment_id', sa.Integer(), nullable=True),
    sa.Column('invoice_type', sa.Enum('PREPAID', 'POSTPAID', name='paymenttype'), nullable=False),
    sa.Column('invoice_number', sa.String(length=50), nullable=False),
    sa.Column('invoice_date', sa.Date(), nullable=False),
    sa.Column('due_date', sa.Date(), nullable=False),
    sa.Column('amount', sa.Numeric(precision=10, scale=2), nullable=False),
    sa.Column('currency', sa.String(length=3), nullable=False),
    sa.Column('tax_amount', sa.Numeric(precision=10, scale=2), nullable=True),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('status', sa.Enum('DRAFT', 'SENT', 'PAID', 'OVERDUE', 'CANCELLED', name='invoicestatus'), nullable=False),
    sa.Column('sent_at', sa.DateTime(), nullable=True),
    sa.Column('paid_at', sa.DateTime(), nullable=True),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
    sa.Column('created_by', sa.Integer(), nullable=True),
    sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
    sa.Column('updated_by', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['created_by'], ['public.user.id'], ),
    sa.ForeignKeyConstraint(['license_period_id'], ['public.license_period.id'], ),
    sa.ForeignKeyConstraint(['payment_id'], ['public.payment.id'], ),
    sa.ForeignKeyConstraint(['updated_by'], ['public.user.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('invoice_number'),
    schema='public'
    )

    with op.batch_alter_table('license_usage', schema=None) as batch_op:
        batch_op.add_column(sa.Column('license_period_id', sa.Integer(), nullable=False))
        batch_op.create_foreign_key(None, 'license_period', ['license_period_id'], ['id'], referent_schema='public')

    # ### end Alembic commands ###


def downgrade():
    pass
    # ### commands auto generated by Alembic - please adjust! ###
    # ### end Alembic commands ###
