-- eResidency Applications Table
CREATE TABLE IF NOT EXISTS eresidency_applications (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  applicant_did VARCHAR(255),
  email VARCHAR(255) NOT NULL,
  given_name VARCHAR(255) NOT NULL,
  family_name VARCHAR(255) NOT NULL,
  date_of_birth DATE,
  nationality VARCHAR(3),
  phone VARCHAR(50),
  address JSONB,
  device_fingerprint VARCHAR(255),
  identity_document JSONB,
  selfie_liveness JSONB,
  status VARCHAR(50) NOT NULL DEFAULT 'draft',
  submitted_at TIMESTAMP,
  reviewed_at TIMESTAMP,
  reviewed_by UUID,
  rejection_reason TEXT,
  kyc_status VARCHAR(50),
  sanctions_status VARCHAR(50),
  pep_status VARCHAR(50),
  risk_score DECIMAL(3, 2),
  risk_assessment JSONB,
  kyc_results JSONB,
  sanctions_results JSONB,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- eCitizenship Applications Table
CREATE TABLE IF NOT EXISTS ecitizenship_applications (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  applicant_did VARCHAR(255) NOT NULL,
  resident_did VARCHAR(255) NOT NULL,
  residency_tenure INTEGER,
  sponsor_did VARCHAR(255),
  service_merit JSONB,
  video_interview JSONB,
  background_attestations JSONB,
  oath_ceremony JSONB,
  status VARCHAR(50) NOT NULL DEFAULT 'draft',
  submitted_at TIMESTAMP,
  reviewed_at TIMESTAMP,
  reviewed_by UUID,
  rejection_reason TEXT,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- Applications Indexes
CREATE INDEX IF NOT EXISTS idx_eresidency_applications_email ON eresidency_applications(email);
CREATE INDEX IF NOT EXISTS idx_eresidency_applications_status ON eresidency_applications(status);
CREATE INDEX IF NOT EXISTS idx_eresidency_applications_applicant_did ON eresidency_applications(applicant_did);
CREATE INDEX IF NOT EXISTS idx_eresidency_applications_created_at ON eresidency_applications(created_at);
CREATE INDEX IF NOT EXISTS idx_ecitizenship_applications_applicant_did ON ecitizenship_applications(applicant_did);
CREATE INDEX IF NOT EXISTS idx_ecitizenship_applications_resident_did ON ecitizenship_applications(resident_did);
CREATE INDEX IF NOT EXISTS idx_ecitizenship_applications_status ON ecitizenship_applications(status);

-- Appeals Table
CREATE TABLE IF NOT EXISTS appeals (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  application_id UUID NOT NULL,
  application_type VARCHAR(50) NOT NULL,
  appellant_did VARCHAR(255) NOT NULL,
  reason TEXT NOT NULL,
  evidence JSONB,
  status VARCHAR(50) NOT NULL DEFAULT 'submitted',
  submitted_at TIMESTAMP NOT NULL DEFAULT NOW(),
  reviewed_at TIMESTAMP,
  reviewed_by UUID,
  decision TEXT,
  decision_date TIMESTAMP,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- Appeals Indexes
CREATE INDEX IF NOT EXISTS idx_appeals_application_id ON appeals(application_id);
CREATE INDEX IF NOT EXISTS idx_appeals_appellant_did ON appeals(appellant_did);
CREATE INDEX IF NOT EXISTS idx_appeals_status ON appeals(status);

-- Review Queue Table
CREATE TABLE IF NOT EXISTS review_queue (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  application_id UUID NOT NULL,
  application_type VARCHAR(50) NOT NULL,
  risk_band VARCHAR(50) NOT NULL,
  risk_score DECIMAL(3, 2),
  assigned_to UUID,
  priority INTEGER DEFAULT 0,
  status VARCHAR(50) NOT NULL DEFAULT 'pending',
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- Review Queue Indexes
CREATE INDEX IF NOT EXISTS idx_review_queue_application_id ON review_queue(application_id);
CREATE INDEX IF NOT EXISTS idx_review_queue_risk_band ON review_queue(risk_band);
CREATE INDEX IF NOT EXISTS idx_review_queue_assigned_to ON review_queue(assigned_to);
CREATE INDEX IF NOT EXISTS idx_review_queue_status ON review_queue(status);
CREATE INDEX IF NOT EXISTS idx_review_queue_priority ON review_queue(priority);

-- Review Actions Audit Table
CREATE TABLE IF NOT EXISTS review_actions_audit (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  application_id UUID NOT NULL,
  reviewer_id UUID NOT NULL,
  action VARCHAR(50) NOT NULL,
  decision VARCHAR(50),
  justification TEXT,
  risk_assessment JSONB,
  metadata JSONB,
  performed_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- Review Actions Audit Indexes
CREATE INDEX IF NOT EXISTS idx_review_actions_application_id ON review_actions_audit(application_id);
CREATE INDEX IF NOT EXISTS idx_review_actions_reviewer_id ON review_actions_audit(reviewer_id);
CREATE INDEX IF NOT EXISTS idx_review_actions_performed_at ON review_actions_audit(performed_at);

