-- Add archival feature to tenders table
-- Allows keeping tender details even after source removes them

ALTER TABLE tenders 
ADD COLUMN IF NOT EXISTS archived BOOLEAN DEFAULT FALSE,
ADD COLUMN IF NOT EXISTS archived_at TIMESTAMP,
ADD COLUMN IF NOT EXISTS archived_snapshot JSONB,
ADD COLUMN IF NOT EXISTS last_validated TIMESTAMP,
ADD COLUMN IF NOT EXISTS validation_failures INTEGER DEFAULT 0;

-- Create index for archival queries
CREATE INDEX IF NOT EXISTS idx_tenders_archived ON tenders(archived) WHERE archived = TRUE;
CREATE INDEX IF NOT EXISTS idx_tenders_last_validated ON tenders(last_validated);

-- Add comment
COMMENT ON COLUMN tenders.archived IS 'TRUE if tender removed from source but we keep snapshot';
COMMENT ON COLUMN tenders.archived_snapshot IS 'Full tender details saved when first scraped';
COMMENT ON COLUMN tenders.last_validated IS 'Last time we verified URL still works';
COMMENT ON COLUMN tenders.validation_failures IS 'Number of consecutive validation failures';
