<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

    <changeSet id="025-seed-production-order-permissions" author="effigenix">
        <preConditions onFail="MARK_RAN">
            <not>
                <sqlCheck expectedResult="1">
                    SELECT COUNT(*) FROM role_permissions
                    WHERE role_id = 'c0a80121-0000-0000-0000-000000000001'
                    AND permission = 'PRODUCTION_ORDER_READ'
                </sqlCheck>
            </not>
        </preConditions>
        <comment>Add PRODUCTION_ORDER_READ and PRODUCTION_ORDER_WRITE permissions for ADMIN and PRODUCTION_MANAGER roles (skipped if already present from 002)</comment>

        <sql dbms="postgresql">
            INSERT INTO role_permissions (role_id, permission) VALUES
                ('c0a80121-0000-0000-0000-000000000001', 'PRODUCTION_ORDER_READ'),
                ('c0a80121-0000-0000-0000-000000000001', 'PRODUCTION_ORDER_WRITE'),
                ('c0a80121-0000-0000-0000-000000000002', 'PRODUCTION_ORDER_READ'),
                ('c0a80121-0000-0000-0000-000000000002', 'PRODUCTION_ORDER_WRITE')
            ON CONFLICT DO NOTHING;
        </sql>
        <sql dbms="h2">
            MERGE INTO role_permissions (role_id, permission) KEY (role_id, permission) VALUES
                ('c0a80121-0000-0000-0000-000000000001', 'PRODUCTION_ORDER_READ');
            MERGE INTO role_permissions (role_id, permission) KEY (role_id, permission) VALUES
                ('c0a80121-0000-0000-0000-000000000001', 'PRODUCTION_ORDER_WRITE');
            MERGE INTO role_permissions (role_id, permission) KEY (role_id, permission) VALUES
                ('c0a80121-0000-0000-0000-000000000002', 'PRODUCTION_ORDER_READ');
            MERGE INTO role_permissions (role_id, permission) KEY (role_id, permission) VALUES
                ('c0a80121-0000-0000-0000-000000000002', 'PRODUCTION_ORDER_WRITE');
        </sql>
    </changeSet>

</databaseChangeLog>
