-- Capsule report system: members voluntarily submit decrypted evidence when reporting
-- encrypted entries. The server stores the plaintext sample provided by the reporter.

CREATE TABLE IF NOT EXISTS capsule_reports (
    id              UUID        PRIMARY KEY DEFAULT gen_random_uuid(),
    reporter_id     UUID        NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
    capsule_id      UUID        NOT NULL REFERENCES groups(id) ON DELETE CASCADE,
    entry_id        UUID        NOT NULL REFERENCES capsule_entries(id) ON DELETE CASCADE,
    decrypted_sample TEXT,      -- plaintext voluntarily provided by the reporter as evidence
    reason          TEXT        NOT NULL,
    status          TEXT        NOT NULL DEFAULT 'pending'
                                CHECK (status IN ('pending', 'reviewed', 'actioned', 'dismissed')),
    created_at      TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at      TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Prevent duplicate reports from the same user for the same entry
CREATE UNIQUE INDEX IF NOT EXISTS uq_capsule_reports_reporter_entry
    ON capsule_reports (reporter_id, entry_id);

CREATE INDEX IF NOT EXISTS idx_capsule_reports_status   ON capsule_reports (status);
CREATE INDEX IF NOT EXISTS idx_capsule_reports_capsule  ON capsule_reports (capsule_id);
