-- Stonks Oracle - Data retention and lifecycle policies
-- Tracks per-bucket and per-artifact-class retention rules.
-- Requirements: N3

CREATE TABLE retention_policies (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    bucket_name VARCHAR(200) NOT NULL,
    artifact_class VARCHAR(100) NOT NULL DEFAULT 'default',
    retention_days INTEGER NOT NULL DEFAULT 365,
    archive_before_delete BOOLEAN NOT NULL DEFAULT FALSE,
    active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    UNIQUE(bucket_name, artifact_class)
);

-- Seed default retention policies per bucket
INSERT INTO retention_policies (bucket_name, artifact_class, retention_days, archive_before_delete) VALUES
    ('stonks-raw-market',  'default', 90,  FALSE),
    ('stonks-raw-news',    'default', 180, FALSE),
    ('stonks-raw-filings', 'default', 365, FALSE),
    ('stonks-normalized',  'default', 180, FALSE),
    ('stonks-llm-prompts', 'default', 365, FALSE),
    ('stonks-llm-results', 'default', 365, FALSE),
    ('stonks-lakehouse',   'default', 730, FALSE),
    ('stonks-audit',       'default', 730, FALSE)
ON CONFLICT (bucket_name, artifact_class) DO NOTHING;

-- Track retention cleanup runs for observability
CREATE TABLE retention_runs (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    bucket_name VARCHAR(200) NOT NULL,
    objects_scanned INTEGER NOT NULL DEFAULT 0,
    objects_deleted INTEGER NOT NULL DEFAULT 0,
    bytes_freed BIGINT NOT NULL DEFAULT 0,
    db_rows_deleted INTEGER NOT NULL DEFAULT 0,
    started_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    completed_at TIMESTAMPTZ,
    status VARCHAR(20) NOT NULL DEFAULT 'running',
    error_message TEXT
);

CREATE INDEX idx_retention_runs_bucket ON retention_runs(bucket_name, started_at DESC);
CREATE INDEX idx_retention_runs_status ON retention_runs(status);
