"""Custom fields: site-defined extra attributes per asset type

Revision ID: 7d14_customfields
Revises: 7d13_printerdrivers
Create Date: 2026-07-09

"""
from alembic import op
import sqlalchemy as sa


revision = '7d14_customfields'
down_revision = '7d13_printerdrivers'
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        'customfields',
        sa.Column('fieldid', sa.Integer(), primary_key=True),
        sa.Column('assettypeid', sa.Integer(), nullable=False),
        sa.Column('fieldkey', sa.String(length=50), nullable=False),
        sa.Column('label', sa.String(length=150), nullable=False),
        sa.Column('datatype', sa.String(length=20), nullable=False, server_default='text'),
        sa.Column('options', sa.Text(), nullable=True),
        sa.Column('showondetail', sa.Boolean(), nullable=False, server_default='1'),
        sa.Column('showonform', sa.Boolean(), nullable=False, server_default='1'),
        sa.Column('sortorder', sa.Integer(), nullable=False, server_default='0'),
        sa.Column('isactive', sa.Boolean(), nullable=False, server_default='1'),
        sa.ForeignKeyConstraint(['assettypeid'], ['assettypes.assettypeid']),
        sa.UniqueConstraint('assettypeid', 'fieldkey', name='uq_customfield_type_key'),
    )
    op.create_table(
        'customfieldvalues',
        sa.Column('valueid', sa.Integer(), primary_key=True),
        sa.Column('fieldid', sa.Integer(), nullable=False),
        sa.Column('assetid', sa.Integer(), nullable=False),
        sa.Column('value', sa.Text(), nullable=True),
        sa.ForeignKeyConstraint(['fieldid'], ['customfields.fieldid'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['assetid'], ['assets.assetid'], ondelete='CASCADE'),
        sa.UniqueConstraint('fieldid', 'assetid', name='uq_customfieldvalue_field_asset'),
    )


def downgrade():
    op.drop_table('customfieldvalues')
    op.drop_table('customfields')
