import { Migration } from '../migrate.js'

export const up: Migration['up'] = async (db) => {
  // Compliance Audit Logs table
  await db.query(`
    CREATE TABLE IF NOT EXISTS compliance_audit_logs (
      id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
      data_id UUID,
      user_id UUID REFERENCES users(id) ON DELETE SET NULL,
      operation VARCHAR(50) NOT NULL,
      region VARCHAR(100),
      framework VARCHAR(50),
      compliant BOOLEAN DEFAULT TRUE,
      violations JSONB DEFAULT '[]'::jsonb,
      timestamp TIMESTAMP WITH TIME ZONE DEFAULT NOW()
    )
  `)

  // Indexes
  await db.query(`
    CREATE INDEX IF NOT EXISTS idx_compliance_audit_data ON compliance_audit_logs(data_id, timestamp)
  `)

  await db.query(`
    CREATE INDEX IF NOT EXISTS idx_compliance_audit_user ON compliance_audit_logs(user_id, timestamp)
  `)

  await db.query(`
    CREATE INDEX IF NOT EXISTS idx_compliance_audit_framework ON compliance_audit_logs(framework, timestamp)
  `)
}

export const down: Migration['down'] = async (db) => {
  await db.query(`DROP TABLE IF EXISTS compliance_audit_logs`)
}

