"""add_qa_supervisor_and_inspector_roles

Revision ID: 558303c44611
Revises: 43020f216ee0
Create Date: 2026-10-13 12:32:40.062306

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '558303c44611'
down_revision: Union[str, None] = '43020f216ee0'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.execute("""
        INSERT INTO role_master (role_id, role_code, role_name, description, is_system_role, is_active, created_at)
        VALUES (
            gen_random_uuid(),
            'QA_SUPERVISOR',
            'QA Supervisor',
            'Plant-wide Quality Assurance, checksheet configuration, and station QA staffing',
            false,
            true,
            NOW()
        )
        ON CONFLICT (role_code) DO NOTHING;

        INSERT INTO role_master (role_id, role_code, role_name, description, is_system_role, is_active, created_at)
        VALUES (
            gen_random_uuid(),
            'QA_INSPECTOR',
            'QA Inspector',
            'Station-level checksheet inspection, defect observation, and photo evidence logging',
            false,
            true,
            NOW()
        )
        ON CONFLICT (role_code) DO NOTHING;

        INSERT INTO role_module_permission (id, role_code, module_code, can_view, can_create, can_edit, can_delete, can_approve, updated_at)
        VALUES
            (gen_random_uuid(), 'QA_SUPERVISOR', 'QA', true, true, true, true, true, NOW()),
            (gen_random_uuid(), 'QA_SUPERVISOR', 'LOCATIONS', true, false, true, false, false, NOW()),
            (gen_random_uuid(), 'QA_INSPECTOR', 'QA', true, true, true, false, false, NOW())
        ON CONFLICT (role_code, module_code) DO NOTHING;
    """)


def downgrade() -> None:
    op.execute("""
        DELETE FROM role_module_permission WHERE role_code IN ('QA_SUPERVISOR', 'QA_INSPECTOR');
        DELETE FROM role_master WHERE role_code IN ('QA_SUPERVISOR', 'QA_INSPECTOR');
    """)
