-- Trinity Console Database Migration
-- Run this AFTER Arbiter 3.0 base tables are created
-- Date: April 1, 2026

-- Track every tier change and subscription lifecycle event
CREATE TABLE IF NOT EXISTS player_history (
    id SERIAL PRIMARY KEY,
    discord_id VARCHAR(255) REFERENCES users(discord_id),
    previous_tier INT,
    new_tier INT,
    change_reason VARCHAR(255), -- 'upgrade', 'downgrade', 'payment_failed', 'manual'
    changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Audit log for The Trinity's actions
CREATE TABLE IF NOT EXISTS admin_audit_log (
    id SERIAL PRIMARY KEY,
    admin_discord_id VARCHAR(255),
    admin_username VARCHAR(255),
    action_type VARCHAR(50), -- 'force_sync', 'manual_role_assign', 'bulk_update', 'tier_change'
    target_identifier VARCHAR(255), -- Server ID or Player Discord ID
    details JSONB,
    performed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Chargeback/ban tracking
CREATE TABLE IF NOT EXISTS banned_users (
    id SERIAL PRIMARY KEY,
    discord_id VARCHAR(255) UNIQUE,
    minecraft_username VARCHAR(255),
    minecraft_uuid VARCHAR(255),
    ban_reason VARCHAR(255), -- 'chargeback', 'tos_violation', 'manual'
    banned_by_discord_id VARCHAR(255),
    banned_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    notes TEXT
);

-- Enhance subscriptions table for MRR, grace period, and referrals
ALTER TABLE subscriptions 
ADD COLUMN IF NOT EXISTS mrr_value DECIMAL(10,2) DEFAULT 0.00,
ADD COLUMN IF NOT EXISTS referrer_discord_id VARCHAR(255),
ADD COLUMN IF NOT EXISTS grace_period_started_at TIMESTAMP,
ADD COLUMN IF NOT EXISTS grace_period_ends_at TIMESTAMP,
ADD COLUMN IF NOT EXISTS payment_failure_reason TEXT,
ADD COLUMN IF NOT EXISTS last_payment_attempt TIMESTAMP;

-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_player_history_discord_id ON player_history(discord_id);
CREATE INDEX IF NOT EXISTS idx_player_history_changed_at ON player_history(changed_at DESC);
CREATE INDEX IF NOT EXISTS idx_audit_log_performed_at ON admin_audit_log(performed_at DESC);
CREATE INDEX IF NOT EXISTS idx_audit_log_admin ON admin_audit_log(admin_discord_id);
CREATE INDEX IF NOT EXISTS idx_subscriptions_grace_period ON subscriptions(grace_period_ends_at) WHERE grace_period_ends_at IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_banned_users_discord_id ON banned_users(discord_id);

-- Performance Indexes for Trinity Console (Added by Chronicler #51)
-- Status is used in WHERE clauses across ALL modules
CREATE INDEX IF NOT EXISTS idx_subscriptions_status 
ON subscriptions(status);

-- Composite index for Grace Period queries (status + grace_period_ends_at)
CREATE INDEX IF NOT EXISTS idx_subscriptions_grace_period_active 
ON subscriptions(status, grace_period_ends_at) 
WHERE status = 'grace_period';

-- Tier level for Financials breakdown
CREATE INDEX IF NOT EXISTS idx_subscriptions_tier_status 
ON subscriptions(tier_level, status);

-- Comments for documentation
COMMENT ON TABLE player_history IS 'Tracks all tier changes and subscription lifecycle events';
COMMENT ON TABLE admin_audit_log IS 'Logs all administrative actions by The Trinity';
COMMENT ON TABLE banned_users IS 'Permanent ban list for chargebacks and TOS violations';
COMMENT ON COLUMN subscriptions.mrr_value IS 'Monthly Recurring Revenue value for this subscription';
COMMENT ON COLUMN subscriptions.grace_period_ends_at IS 'When the 3-day grace period ends after payment failure';
