-- Migration: Create platform change log table for admin audit trail
-- Feature: admin
-- Description: Tracks all changes to platform catalog data (makes, models, years, trims, engines)

CREATE TABLE IF NOT EXISTS platform_change_log (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  change_type VARCHAR(50) NOT NULL CHECK (change_type IN ('CREATE', 'UPDATE', 'DELETE')),
  resource_type VARCHAR(100) NOT NULL CHECK (resource_type IN ('makes', 'models', 'years', 'trims', 'engines')),
  resource_id VARCHAR(255),
  old_value JSONB,
  new_value JSONB,
  changed_by VARCHAR(255) NOT NULL,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- Index for querying by resource type and date
CREATE INDEX IF NOT EXISTS idx_platform_change_log_resource_type ON platform_change_log(resource_type);
CREATE INDEX IF NOT EXISTS idx_platform_change_log_created_at ON platform_change_log(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_platform_change_log_changed_by ON platform_change_log(changed_by);

-- Index for finding changes to specific resources
CREATE INDEX IF NOT EXISTS idx_platform_change_log_resource ON platform_change_log(resource_type, resource_id);
