"""measuringtools plugin baseline (real create).

This plugin was built AFTER the ADR-008 ownership cutover, so unlike the ten
bundled plugins whose 0001 is a stamp-only no-op anchor, this baseline actually
CREATES the plugin's tables. The core Alembic chain never knew about
measuringtooltypes / measuringtools, so this per-plugin chain is their sole
authoritative creator. It runs from `flask plugin install measuringtools`
(and `flask plugin upgrade-all`) after `flask db upgrade` builds the core schema.

Note on create_plugin_tables: the shared helper in
shopdb.plugins.alembic_template builds a per-plugin MetaData filtered to only the
plugin's own tables, which means a foreign key to a core table (assets) cannot
be resolved at CreateTable-compile time (NoReferencedTableError). Since these
tables reference assets.assetid, the baseline emits explicit Alembic ops (the
same shape Alembic autogenerate produces) instead. Tables inherit the
connection's default charset, so on a utf8mb4 database they are utf8mb4, matching
how the core chain creates its tables.
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'measuringtools0001baseline'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        'measuringtooltypes',
        sa.Column('measuringtooltypeid', sa.Integer(), nullable=False),
        sa.Column('name', sa.String(length=100), nullable=False),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('color', sa.String(length=20), nullable=True),
        sa.Column('createddate', sa.DateTime(), nullable=False),
        sa.Column('modifieddate', sa.DateTime(), nullable=False),
        sa.Column('isactive', sa.Boolean(), nullable=False),
        sa.PrimaryKeyConstraint('measuringtooltypeid'),
        sa.UniqueConstraint('name'),
    )
    op.create_table(
        'measuringtools',
        sa.Column('measuringtoolid', sa.Integer(), nullable=False),
        sa.Column('assetid', sa.Integer(), nullable=False),
        sa.Column('measuringtooltypeid', sa.Integer(), nullable=True),
        sa.Column('calibrationintervaldays', sa.Integer(), nullable=True),
        sa.Column('lastcalibrationdate', sa.Date(), nullable=True),
        sa.Column('nextcalibrationdate', sa.Date(), nullable=True),
        sa.Column('calibrationprovider', sa.String(length=150), nullable=True),
        sa.Column('notes', sa.Text(), nullable=True),
        sa.Column('createddate', sa.DateTime(), nullable=False),
        sa.Column('modifieddate', sa.DateTime(), nullable=False),
        sa.Column('isactive', sa.Boolean(), nullable=False),
        sa.ForeignKeyConstraint(['assetid'], ['assets.assetid'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['measuringtooltypeid'],
                                ['measuringtooltypes.measuringtooltypeid']),
        sa.PrimaryKeyConstraint('measuringtoolid'),
        sa.UniqueConstraint('assetid'),
    )
    op.create_index('idx_measuringtool_type', 'measuringtools',
                    ['measuringtooltypeid'])


def downgrade():
    op.drop_index('idx_measuringtool_type', table_name='measuringtools')
    op.drop_table('measuringtools')
    op.drop_table('measuringtooltypes')
