-- Migration: Add tiered retention classification columns
-- Description: Adds categories array and expires_at for tiered backup retention
-- Issue: #6 - Backup retention purges all backups

-- ============================================
-- Add new columns to backup_history
-- ============================================
ALTER TABLE backup_history
ADD COLUMN IF NOT EXISTS categories TEXT[] DEFAULT '{}',
ADD COLUMN IF NOT EXISTS expires_at TIMESTAMP WITH TIME ZONE;

-- ============================================
-- Indexes for efficient category queries
-- ============================================
CREATE INDEX IF NOT EXISTS idx_backup_history_categories ON backup_history USING GIN(categories);
CREATE INDEX IF NOT EXISTS idx_backup_history_expires ON backup_history(expires_at);

-- ============================================
-- Populate categories for existing backups based on started_at
-- Classification logic:
--   - All backups: 'hourly'
--   - Hour = 0 (midnight UTC): + 'daily'
--   - Hour = 0 AND Sunday: + 'weekly'
--   - Hour = 0 AND day = 1: + 'monthly'
-- ============================================
UPDATE backup_history
SET categories = ARRAY(
    SELECT unnest(
        CASE
            -- Midnight on Sunday, 1st of month: all categories
            WHEN EXTRACT(HOUR FROM started_at AT TIME ZONE 'UTC') = 0
                 AND EXTRACT(DOW FROM started_at AT TIME ZONE 'UTC') = 0
                 AND EXTRACT(DAY FROM started_at AT TIME ZONE 'UTC') = 1
            THEN ARRAY['hourly', 'daily', 'weekly', 'monthly']

            -- Midnight on Sunday (not 1st): hourly + daily + weekly
            WHEN EXTRACT(HOUR FROM started_at AT TIME ZONE 'UTC') = 0
                 AND EXTRACT(DOW FROM started_at AT TIME ZONE 'UTC') = 0
            THEN ARRAY['hourly', 'daily', 'weekly']

            -- Midnight on 1st (not Sunday): hourly + daily + monthly
            WHEN EXTRACT(HOUR FROM started_at AT TIME ZONE 'UTC') = 0
                 AND EXTRACT(DAY FROM started_at AT TIME ZONE 'UTC') = 1
            THEN ARRAY['hourly', 'daily', 'monthly']

            -- Midnight (not Sunday, not 1st): hourly + daily
            WHEN EXTRACT(HOUR FROM started_at AT TIME ZONE 'UTC') = 0
            THEN ARRAY['hourly', 'daily']

            -- Non-midnight: hourly only
            ELSE ARRAY['hourly']
        END
    )
)
WHERE categories = '{}' OR categories IS NULL;

-- ============================================
-- Calculate expires_at based on categories
-- Retention periods: hourly=8hrs, daily=7days, weekly=4wks, monthly=12mo
-- Use longest applicable retention period
-- ============================================
UPDATE backup_history
SET expires_at = CASE
    WHEN 'monthly' = ANY(categories) THEN started_at + INTERVAL '12 months'
    WHEN 'weekly' = ANY(categories) THEN started_at + INTERVAL '4 weeks'
    WHEN 'daily' = ANY(categories) THEN started_at + INTERVAL '7 days'
    ELSE started_at + INTERVAL '8 hours'
END
WHERE expires_at IS NULL;

-- ============================================
-- Add NOT NULL constraint after populating data
-- ============================================
ALTER TABLE backup_history
ALTER COLUMN categories SET DEFAULT ARRAY['hourly']::TEXT[];

-- Ensure all rows have categories
UPDATE backup_history SET categories = ARRAY['hourly'] WHERE categories = '{}' OR categories IS NULL;
