-- Migration 025: Tender Analysis Schema
-- Stores uploaded tender documents, extracted requirements, and control matching results

CREATE TABLE IF NOT EXISTS tender_analyses (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id UUID NOT NULL,

    -- Document
    file_name VARCHAR(500) NOT NULL,
    file_size BIGINT DEFAULT 0,
    file_content BYTEA,

    -- Project
    project_name VARCHAR(500),
    customer_name VARCHAR(500),

    -- Status
    status VARCHAR(50) DEFAULT 'uploaded',
    -- CHECK (status IN ('uploaded', 'extracting', 'extracted', 'matched', 'completed', 'error'))

    -- Extracted requirements
    requirements JSONB DEFAULT '[]'::jsonb,
    total_requirements INT DEFAULT 0,

    -- Match results
    match_results JSONB DEFAULT '[]'::jsonb,
    matched_count INT DEFAULT 0,
    unmatched_count INT DEFAULT 0,
    partial_count INT DEFAULT 0,

    -- Audit
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_ta_tenant ON tender_analyses (tenant_id);
CREATE INDEX IF NOT EXISTS idx_ta_status ON tender_analyses (status);
