-- =============================================================================
-- Migration 003: Document Import Tables
--
-- Tables for imported compliance documents and gap analysis results.
-- =============================================================================

CREATE TABLE IF NOT EXISTS compliance_imported_documents (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id VARCHAR(255) NOT NULL,
    filename VARCHAR(500) NOT NULL,
    file_type VARCHAR(50) NOT NULL,
    file_size INTEGER,
    detected_type VARCHAR(50),
    detection_confidence FLOAT,
    extracted_text TEXT,
    extracted_entities JSONB DEFAULT '[]',
    recommendations JSONB DEFAULT '[]',
    status VARCHAR(20) DEFAULT 'pending',
    analyzed_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_imported_docs_tenant ON compliance_imported_documents(tenant_id);
CREATE INDEX IF NOT EXISTS idx_imported_docs_status ON compliance_imported_documents(status);

CREATE TABLE IF NOT EXISTS compliance_gap_analyses (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id VARCHAR(255) NOT NULL,
    document_id UUID REFERENCES compliance_imported_documents(id) ON DELETE CASCADE,
    total_gaps INTEGER DEFAULT 0,
    critical_gaps INTEGER DEFAULT 0,
    high_gaps INTEGER DEFAULT 0,
    medium_gaps INTEGER DEFAULT 0,
    low_gaps INTEGER DEFAULT 0,
    gaps JSONB DEFAULT '[]',
    recommended_packages JSONB DEFAULT '[]',
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_gap_analyses_tenant ON compliance_gap_analyses(tenant_id);
CREATE INDEX IF NOT EXISTS idx_gap_analyses_document ON compliance_gap_analyses(document_id);
