-- Migration: 078_nextcloud_audit_log
-- Created: 2026-02-01

CREATE TABLE IF NOT EXISTS nextcloud_audit_log (
  id BIGSERIAL NOT NULL,
  customer_id INTEGER NOT NULL REFERENCES customers(id) ON DELETE CASCADE,
  instance_id INTEGER REFERENCES nextcloud_instances(id) ON DELETE SET NULL,
  event_type VARCHAR(50) NOT NULL,
  request_meta JSONB,
  response_meta JSONB,
  actor_user_id INTEGER,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  PRIMARY KEY (id, created_at)
) PARTITION BY RANGE (created_at);

-- Create current month partition
CREATE TABLE IF NOT EXISTS nextcloud_audit_log_2026_02
  PARTITION OF nextcloud_audit_log
  FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');

CREATE INDEX IF NOT EXISTS idx_nextcloud_audit_customer_created
  ON nextcloud_audit_log (customer_id, created_at DESC);
