"""software subtasks and jobcard advance billing

Revision ID: e4c1b82f9a10
Revises: d7a1c5e92f44
Create Date: 2026-09-07
"""
from alembic import op
import sqlalchemy as sa

revision = 'e4c1b82f9a10'
down_revision = 'd7a1c5e92f44'
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        'software_subtasks',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('assignment_id', sa.Integer(), nullable=False),
        sa.Column('title', sa.String(length=220), nullable=False),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('priority', sa.String(length=20), nullable=False, server_default='NORMAL'),
        sa.Column('status', sa.String(length=20), nullable=False, server_default='TODO'),
        sa.Column('due_date', sa.Date(), nullable=True),
        sa.Column('created_by_id', sa.Integer(), nullable=False),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.Column('updated_at', sa.DateTime(), nullable=False),
        sa.Column('completed_at', sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(['assignment_id'], ['software_assignments.id']),
        sa.ForeignKeyConstraint(['created_by_id'], ['users.id']),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_software_subtasks_assignment_id', 'software_subtasks', ['assignment_id'], unique=False)
    op.create_index('ix_software_subtasks_status', 'software_subtasks', ['status'], unique=False)
    op.create_index('ix_software_subtasks_due_date', 'software_subtasks', ['due_date'], unique=False)

    with op.batch_alter_table('software_time_entries') as batch_op:
        batch_op.add_column(sa.Column('subtask_id', sa.Integer(), nullable=True))
        batch_op.create_index('ix_software_time_entries_subtask_id', ['subtask_id'], unique=False)
        batch_op.create_foreign_key('fk_software_time_entries_subtask_id', 'software_subtasks', ['subtask_id'], ['id'])

    op.create_table(
        'jobcard_advance_billings',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('jobcard_id', sa.Integer(), nullable=False),
        sa.Column('quote_id', sa.Integer(), nullable=False),
        sa.Column('requested_amount', sa.Numeric(14, 2), nullable=False),
        sa.Column('currency', sa.String(length=10), nullable=False, server_default='INR'),
        sa.Column('note', sa.Text(), nullable=True),
        sa.Column('status', sa.String(length=30), nullable=False, server_default='PI_REQUESTED'),
        sa.Column('requested_by_id', sa.Integer(), nullable=False),
        sa.Column('requested_at', sa.DateTime(), nullable=False),
        sa.Column('invoice_id', sa.Integer(), nullable=True),
        sa.Column('invoiced_at', sa.DateTime(), nullable=True),
        sa.Column('cancelled_at', sa.DateTime(), nullable=True),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.Column('updated_at', sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(['jobcard_id'], ['operations_jobcard.id']),
        sa.ForeignKeyConstraint(['quote_id'], ['quotes.id']),
        sa.ForeignKeyConstraint(['requested_by_id'], ['users.id']),
        sa.ForeignKeyConstraint(['invoice_id'], ['invoices.id']),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('invoice_id')
    )
    op.create_index('ix_jobcard_advance_billings_jobcard_id', 'jobcard_advance_billings', ['jobcard_id'], unique=False)
    op.create_index('ix_jobcard_advance_billings_quote_id', 'jobcard_advance_billings', ['quote_id'], unique=False)
    op.create_index('ix_jobcard_advance_billings_status', 'jobcard_advance_billings', ['status'], unique=False)
    op.create_index('ix_jobcard_advance_billings_invoice_id', 'jobcard_advance_billings', ['invoice_id'], unique=True)

    with op.batch_alter_table('proforma_invoices') as batch_op:
        batch_op.add_column(sa.Column('advance_billing_id', sa.Integer(), nullable=True))
        batch_op.create_index('ix_proforma_invoices_advance_billing_id', ['advance_billing_id'], unique=False)
        batch_op.create_foreign_key('fk_proforma_invoices_advance_billing_id', 'jobcard_advance_billings', ['advance_billing_id'], ['id'])


def downgrade():
    with op.batch_alter_table('proforma_invoices') as batch_op:
        batch_op.drop_constraint('fk_proforma_invoices_advance_billing_id', type_='foreignkey')
        batch_op.drop_index('ix_proforma_invoices_advance_billing_id')
        batch_op.drop_column('advance_billing_id')

    op.drop_index('ix_jobcard_advance_billings_invoice_id', table_name='jobcard_advance_billings')
    op.drop_index('ix_jobcard_advance_billings_status', table_name='jobcard_advance_billings')
    op.drop_index('ix_jobcard_advance_billings_quote_id', table_name='jobcard_advance_billings')
    op.drop_index('ix_jobcard_advance_billings_jobcard_id', table_name='jobcard_advance_billings')
    op.drop_table('jobcard_advance_billings')

    with op.batch_alter_table('software_time_entries') as batch_op:
        batch_op.drop_constraint('fk_software_time_entries_subtask_id', type_='foreignkey')
        batch_op.drop_index('ix_software_time_entries_subtask_id')
        batch_op.drop_column('subtask_id')

    op.drop_index('ix_software_subtasks_due_date', table_name='software_subtasks')
    op.drop_index('ix_software_subtasks_status', table_name='software_subtasks')
    op.drop_index('ix_software_subtasks_assignment_id', table_name='software_subtasks')
    op.drop_table('software_subtasks')
