<?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-4.27.xsd">

  <changeSet id="0002-1-dictionary-definitions" author="ordinis">
    <comment>Каталог справочников: имя, JSON Schema, scope, версия.</comment>
    <createTable tableName="dictionary_definitions">
      <column name="id" type="UUID" defaultValueComputed="gen_random_uuid()">
        <constraints primaryKey="true" nullable="false"/>
      </column>
      <column name="name" type="VARCHAR(128)">
        <constraints nullable="false" unique="true"/>
      </column>
      <column name="display_name" type="VARCHAR(256)"/>
      <column name="description" type="TEXT"/>
      <column name="scope" type="VARCHAR(32)">
        <constraints nullable="false"/>
      </column>
      <column name="schema_json" type="JSONB">
        <constraints nullable="false"/>
      </column>
      <column name="schema_version" type="VARCHAR(32)" defaultValue="1.0.0">
        <constraints nullable="false"/>
      </column>
      <column name="bundle" type="VARCHAR(64)" defaultValue="cuod">
        <constraints nullable="false"/>
      </column>
      <column name="supported_locales" type="TEXT[]" defaultValueComputed="ARRAY['ru-RU']::TEXT[]">
        <constraints nullable="false"/>
      </column>
      <column name="default_locale" type="VARCHAR(10)" defaultValue="ru-RU">
        <constraints nullable="false"/>
      </column>
      <column name="created_at" type="TIMESTAMPTZ" defaultValueComputed="NOW()">
        <constraints nullable="false"/>
      </column>
      <column name="updated_at" type="TIMESTAMPTZ" defaultValueComputed="NOW()">
        <constraints nullable="false"/>
      </column>
      <column name="created_by" type="VARCHAR(128)"/>
      <column name="updated_by" type="VARCHAR(128)"/>
    </createTable>

    <sql>
      ALTER TABLE dictionary_definitions
      ADD CONSTRAINT dictionary_definitions_scope_check
      CHECK (scope IN ('public', 'internal', 'restricted'));
    </sql>

    <createIndex indexName="idx_dict_def_bundle" tableName="dictionary_definitions">
      <column name="bundle"/>
    </createIndex>
  </changeSet>

</databaseChangeLog>
