-- Migration to remove text extraction functionality from the database
-- This drops all text extraction related tables, views, and functions
-- NOTE: Field extractions are separate and NOT affected by this migration

-- First drop the view that depends on text extraction tables
DROP VIEW IF EXISTS currentTextEntries;

-- Drop fullActiveCollectors view that depends on currentCollectorMinTextVersions
DROP VIEW IF EXISTS fullActiveCollectors;

-- Drop the currentCollectorMinTextVersions view
DROP VIEW IF EXISTS currentCollectorMinTextVersions;

-- Drop the trigger and function for collectorMinTextVersions
DROP TRIGGER IF EXISTS removeMinTextVersionTrigger ON collectorMinTextVersions;
DROP FUNCTION IF EXISTS removeMinTextVersion;

-- Drop the collectorMinTextVersions table
DROP TABLE IF EXISTS collectorMinTextVersions;

-- Drop the text extraction tables (child first for FK constraints)
DROP TABLE IF EXISTS documentTextExtractionEntries;
DROP TABLE IF EXISTS documentTextExtractions;

-- Recreate fullActiveCollectors view WITHOUT minTextVersion
CREATE VIEW fullActiveCollectors AS
    SELECT DISTINCT av.clientId,
    ccv.minCleanVersion,
    av.activeVersion, lv.latestVersion
    FROM collectorCurrentActiveVersions as av
    JOIN collectorLatestVersions as lv on lv.clientId = av.clientId
    JOIN currentCollectorMinCleanVersions AS ccv ON av.clientId = ccv.clientId;
