<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    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
                        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

    <!--
        Added the entity SecurityResource.
    -->
    <changeSet id="20250402200000-1" author="omg">
        <createTable tableName="security_resource" remarks="Security Resource\nTable with app resources that can be subject to security permission configuration.">
            <column name="id" type="bigint" autoIncrement="true" startWith="1">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="code" type="varchar(255)" remarks="User defined code. Must be unique.">
                <constraints nullable="false" unique="true" uniqueConstraintName="ux_security_resource__code" />
            </column>
            <column name="name" type="varchar(255)" remarks="The name of the SecurityResource. User friendly name.">
                <constraints nullable="false" />
            </column>
            <column name="type" type="varchar(15)" remarks="A type to identify this resource. (see enum SecurityResourceType)">
                <constraints nullable="false" />
            </column>
            
            <column name="default_create_permission" type="varchar(15)" remarks="A default permission for the resource in the scope : create.">
                <constraints nullable="false" />
            </column>
            <column name="default_read_permission" type="varchar(15)" remarks="A default permission for the resource in the scope : read.">
                <constraints nullable="false" />
            </column>
            <column name="default_update_permission" type="varchar(15)" remarks="A default permission for the resource in the scope : update.">
                <constraints nullable="false" />
            </column>
            <column name="default_delete_permission" type="varchar(15)" remarks="A default permission for the resource in the scope : delete.">
                <constraints nullable="false" />
            </column>
            
            <column name="version" type="integer" defaultValueNumeric="-1" remarks="Record version for concurrency control.">
                <constraints nullable="false" />
            </column>
        </createTable>
    </changeSet>
    
    <!--
        Added the entity SecurityGroup.
    -->
    <changeSet id="20250402200000-2" author="omg">
        <createTable tableName="security_group" remarks="Security Group\nA group of permissions.">
            <column name="id" type="bigint" autoIncrement="true" startWith="1">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="code" type="varchar(255)" remarks="User defined code. Must be unique.">
                <constraints nullable="false" unique="true" uniqueConstraintName="ux_security_group__code" />
            </column>
            <column name="name" type="varchar(255)" remarks="The name of the SecurityGroup. User friendly name.">
                <constraints nullable="false" />
            </column>
            
            <column name="version" type="integer" defaultValueNumeric="-1" remarks="Record version for concurrency control.">
                <constraints nullable="false" />
            </column>
        </createTable>
    </changeSet>

    <!--
        Added the entity SecurityGroupPermission.
    -->
    <changeSet id="20250402200000-3" author="omg">
        <createTable tableName="security_group_permission" remarks="Security Group Permission\nThe list of permissions for the group.">
            <column name="id" type="bigint" autoIncrement="true" startWith="1">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            
            <column name="security_group_id" type="bigint">
	        	<constraints nullable="false" />
	        </column>
            
            <column name="security_resource_id" type="bigint">
	        	<constraints nullable="false" />
	        </column>
	        
            <column name="create_permission" type="varchar(15)" remarks="Permission for the resource in the scope : create.">
                <constraints nullable="false" />
            </column>
            <column name="read_permission" type="varchar(15)" remarks="Permission for the resource in the scope : read.">
                <constraints nullable="false" />
            </column>
            <column name="update_permission" type="varchar(15)" remarks="Permission for the resource in the scope : update.">
                <constraints nullable="false" />
            </column>
            <column name="delete_permission" type="varchar(15)" remarks="Permission for the resource in the scope : delete.">
                <constraints nullable="false" />
            </column>
            
            <column name="version" type="integer" defaultValueNumeric="-1" remarks="Record version for concurrency control.">
                <constraints nullable="false" />
            </column>
        </createTable>
        
        <addForeignKeyConstraint
	        baseTableName="security_group_permission"
	        baseColumnNames="security_group_id"
	        referencedTableName="security_group"
	        referencedColumnNames="id"
	        constraintName="fk_security_group_permission__security_group_id"/>
	        
        <addForeignKeyConstraint
	        baseTableName="security_group_permission"
	        baseColumnNames="security_resource_id"
	        referencedTableName="security_resource"
	        referencedColumnNames="id"
	        constraintName="fk_security_group_permission__security_resource_id"/>
    </changeSet>
    
    
</databaseChangeLog>
