<?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="0004-1-audit-log" author="ordinis">
    <comment>Юридически значимый audit log. Source of truth для compliance, отдельно от Loki.</comment>
    <createTable tableName="audit_log">
      <column name="id" type="BIGSERIAL">
        <constraints primaryKey="true" nullable="false"/>
      </column>
      <column name="event_time" type="TIMESTAMPTZ" defaultValueComputed="NOW()">
        <constraints nullable="false"/>
      </column>
      <column name="event_type" type="VARCHAR(64)">
        <constraints nullable="false"/>
      </column>
      <column name="user_id" type="VARCHAR(128)"/>
      <column name="user_scope" type="VARCHAR(32)"/>
      <column name="dictionary_id" type="UUID"/>
      <column name="dictionary_name" type="VARCHAR(128)"/>
      <column name="record_id" type="UUID"/>
      <column name="business_key" type="VARCHAR(256)"/>
      <column name="action" type="VARCHAR(32)"/>
      <column name="payload_before" type="JSONB"/>
      <column name="payload_after" type="JSONB"/>
      <column name="trace_id" type="VARCHAR(64)"/>
      <column name="request_id" type="VARCHAR(64)"/>
      <column name="ip_address" type="INET"/>
      <column name="user_agent" type="TEXT"/>
    </createTable>

    <createIndex indexName="idx_audit_event_time" tableName="audit_log">
      <column name="event_time"/>
    </createIndex>
    <createIndex indexName="idx_audit_user_id" tableName="audit_log">
      <column name="user_id"/>
    </createIndex>
    <createIndex indexName="idx_audit_dictionary" tableName="audit_log">
      <column name="dictionary_id"/>
    </createIndex>
    <createIndex indexName="idx_audit_trace" tableName="audit_log">
      <column name="trace_id"/>
    </createIndex>
  </changeSet>

</databaseChangeLog>
