"""Fold remaining plugin schema into the core chain

Deploys run `flask db upgrade` (core chain) only, so the core chain must
reproduce the full bundled schema. The core baseline already creates the
plugin tables as of baseline time; this adds the post-baseline additions that
previously lived only in per-plugin migrations (which deploys don't run):

  - modelsupplies (printer model -> toner/drum/waste part numbers)
  - computers.vendorid + computers.modelnumberid (PC make/model)
  - computerinstalledapps.installedversion (collector version string)

Idempotent: skips anything already present, so it is a no-op on the live DB
(which already has these) and creates them on a fresh deploy.

Revision ID: 7c04_fold_plugin_schema
Revises: 7c03_locationtypes
Create Date: 2026-06-26

"""
from alembic import op
import sqlalchemy as sa


revision = '7c04_fold_plugin_schema'
down_revision = '7c03_locationtypes'
branch_labels = None
depends_on = None


def upgrade():
    bind = op.get_bind()
    insp = sa.inspect(bind)
    tables = set(insp.get_table_names())

    if 'modelsupplies' not in tables:
        op.create_table(
            'modelsupplies',
            sa.Column('modelsupplyid', sa.Integer(), primary_key=True),
            sa.Column('modelnumberid', sa.Integer(),
                      sa.ForeignKey('models.modelnumberid'), nullable=False),
            sa.Column('supplytype', sa.String(length=20), nullable=False,
                      server_default='toner'),
            sa.Column('color', sa.String(length=20), nullable=False,
                      server_default='none'),
            sa.Column('capacitytier', sa.String(length=20), nullable=False,
                      server_default='standard'),
            sa.Column('partnumber', sa.String(length=50), nullable=False),
            sa.Column('marketingname', sa.String(length=120), nullable=True),
            sa.Column('pageyield', sa.Integer(), nullable=True),
            sa.Column('notes', sa.Text(), nullable=True),
            sa.Column('createddate', sa.DateTime(), nullable=True),
            sa.Column('modifieddate', sa.DateTime(), nullable=True),
            sa.Column('isactive', sa.Boolean(), nullable=True),
            sa.UniqueConstraint('modelnumberid', 'partnumber',
                                name='uq_modelsupply_part'),
        )
        op.create_index('idx_modelsupplies_modelnumberid', 'modelsupplies',
                        ['modelnumberid'])

    comp_cols = {c['name'] for c in insp.get_columns('computers')}
    with op.batch_alter_table('computers') as batch_op:
        if 'vendorid' not in comp_cols:
            batch_op.add_column(sa.Column('vendorid', sa.Integer(), nullable=True))
            batch_op.create_foreign_key('fk_computers_vendor', 'vendors',
                                        ['vendorid'], ['vendorid'])
        if 'modelnumberid' not in comp_cols:
            batch_op.add_column(sa.Column('modelnumberid', sa.Integer(), nullable=True))
            batch_op.create_foreign_key('fk_computers_model', 'models',
                                        ['modelnumberid'], ['modelnumberid'])

    cia_cols = {c['name'] for c in insp.get_columns('computerinstalledapps')}
    if 'installedversion' not in cia_cols:
        op.add_column('computerinstalledapps',
                      sa.Column('installedversion', sa.String(length=100),
                                nullable=True))


def downgrade():
    op.drop_table('modelsupplies')
    with op.batch_alter_table('computers') as batch_op:
        batch_op.drop_constraint('fk_computers_model', type_='foreignkey')
        batch_op.drop_constraint('fk_computers_vendor', type_='foreignkey')
        batch_op.drop_column('modelnumberid')
        batch_op.drop_column('vendorid')
    op.drop_column('computerinstalledapps', 'installedversion')
