-- ============================================================================
-- StellaOps Migration Reset Script for Pre-1.0 Deployments
-- ============================================================================
-- This script updates schema_migrations tables to recognize the 1.0.0 compacted
-- migrations for deployments that upgraded from pre-1.0 versions.
--
-- Run via: psql -f migrations-reset-pre-1.0.sql
-- Or with connection: psql -h <host> -U <user> -d <db> -f migrations-reset-pre-1.0.sql
-- ============================================================================

BEGIN;

-- ============================================================================
-- Authority Module Reset
-- ============================================================================
-- Original: 001_initial_schema, 002_mongo_store_equivalents, 003_enable_rls,
--          004_offline_kit_audit, 005_verdict_manifests
-- New: 001_initial_schema (compacted)

DELETE FROM authority.schema_migrations
WHERE migration_name IN (
    '001_initial_schema.sql',
    '002_mongo_store_equivalents.sql',
    '003_enable_rls.sql',
    '004_offline_kit_audit.sql',
    '005_verdict_manifests.sql'
);

INSERT INTO authority.schema_migrations (migration_name, category, checksum, applied_at)
VALUES ('001_initial_schema.sql', 'startup', 'compacted_1.0.0', NOW())
ON CONFLICT (migration_name) DO NOTHING;

-- ============================================================================
-- Scheduler Module Reset
-- ============================================================================
-- Original: 001_initial_schema, 002_graph_jobs, 003_runs_policy,
--          010_generated_columns_runs, 011_enable_rls, 012_partition_audit,
--          012b_migrate_audit_data
-- New: 001_initial_schema (compacted)

DELETE FROM scheduler.schema_migrations
WHERE migration_name IN (
    '001_initial_schema.sql',
    '002_graph_jobs.sql',
    '003_runs_policy.sql',
    '010_generated_columns_runs.sql',
    '011_enable_rls.sql',
    '012_partition_audit.sql',
    '012b_migrate_audit_data.sql'
);

INSERT INTO scheduler.schema_migrations (migration_name, category, checksum, applied_at)
VALUES ('001_initial_schema.sql', 'startup', 'compacted_1.0.0', NOW())
ON CONFLICT (migration_name) DO NOTHING;

-- ============================================================================
-- Scanner Module Reset
-- ============================================================================
-- Original: 001-034 plus various numbered files (27 total)
-- New: 001_initial_schema (compacted)

DELETE FROM scanner.schema_migrations
WHERE migration_name IN (
    '001_create_tables.sql',
    '002_proof_spine_tables.sql',
    '003_classification_history.sql',
    '004_scan_metrics.sql',
    '005_smart_diff_tables.sql',
    '006_score_replay_tables.sql',
    '007_unknowns_ranking_containment.sql',
    '008_epss_integration.sql',
    '0059_scans_table.sql',
    '0065_unknowns_table.sql',
    '0075_scan_findings_table.sql',
    '020_call_graph_tables.sql',
    '021_smart_diff_tables_search_path.sql',
    '022_reachability_drift_tables.sql',
    '023_scanner_api_ingestion.sql',
    '024_smart_diff_priority_score_widen.sql',
    '025_epss_raw_layer.sql',
    '026_epss_signal_layer.sql',
    '027_witness_storage.sql',
    '028_epss_triage_columns.sql',
    '029_vuln_surfaces.sql',
    '030_vuln_surface_triggers_update.sql',
    '031_reach_cache.sql',
    '032_idempotency_keys.sql',
    '033_binary_evidence.sql',
    '034_func_proof_tables.sql',
    'DM001_rename_scanner_migrations.sql'
);

INSERT INTO scanner.schema_migrations (migration_name, category, checksum, applied_at)
VALUES ('001_initial_schema.sql', 'startup', 'compacted_1.0.0', NOW())
ON CONFLICT (migration_name) DO NOTHING;

-- ============================================================================
-- Policy Module Reset
-- ============================================================================
-- Original: 001-013 (14 files, includes duplicate 010 prefix)
-- New: 001_initial_schema (compacted)

DELETE FROM policy.schema_migrations
WHERE migration_name IN (
    '001_initial_schema.sql',
    '002_cvss_receipts.sql',
    '003_snapshots_violations.sql',
    '004_epss_risk_scores.sql',
    '005_cvss_multiversion.sql',
    '006_enable_rls.sql',
    '007_unknowns_registry.sql',
    '008_exception_objects.sql',
    '009_exception_applications.sql',
    '010_recheck_evidence.sql',
    '010_unknowns_blast_radius_containment.sql',
    '011_unknowns_reason_codes.sql',
    '012_budget_ledger.sql',
    '013_exception_approval.sql'
);

INSERT INTO policy.schema_migrations (migration_name, category, checksum, applied_at)
VALUES ('001_initial_schema.sql', 'startup', 'compacted_1.0.0', NOW())
ON CONFLICT (migration_name) DO NOTHING;

-- ============================================================================
-- Notify Module Reset
-- ============================================================================
-- Original: 001_initial_schema, 010_enable_rls, 011_partition_deliveries,
--          011b_migrate_deliveries_data
-- New: 001_initial_schema (compacted)

DELETE FROM notify.schema_migrations
WHERE migration_name IN (
    '001_initial_schema.sql',
    '010_enable_rls.sql',
    '011_partition_deliveries.sql',
    '011b_migrate_deliveries_data.sql'
);

INSERT INTO notify.schema_migrations (migration_name, category, checksum, applied_at)
VALUES ('001_initial_schema.sql', 'startup', 'compacted_1.0.0', NOW())
ON CONFLICT (migration_name) DO NOTHING;

-- ============================================================================
-- Concelier Module Reset
-- ============================================================================
-- Original: 17 migration files
-- New: 001_initial_schema (compacted)

DELETE FROM concelier.schema_migrations
WHERE migration_name ~ '^[0-9]{3}_.*\.sql$';

INSERT INTO concelier.schema_migrations (migration_name, category, checksum, applied_at)
VALUES ('001_initial_schema.sql', 'startup', 'compacted_1.0.0', NOW())
ON CONFLICT (migration_name) DO NOTHING;

-- ============================================================================
-- Attestor Module Reset (proofchain + attestor schemas)
-- ============================================================================
-- Original: 20251214000001_AddProofChainSchema.sql, 20251216_001_create_rekor_submission_queue.sql
-- New: 001_initial_schema (compacted)

DELETE FROM proofchain.schema_migrations
WHERE migration_name IN (
    '20251214000001_AddProofChainSchema.sql',
    '20251214000002_RollbackProofChainSchema.sql',
    '20251216_001_create_rekor_submission_queue.sql'
);

INSERT INTO proofchain.schema_migrations (migration_name, category, checksum, applied_at)
VALUES ('001_initial_schema.sql', 'startup', 'compacted_1.0.0', NOW())
ON CONFLICT (migration_name) DO NOTHING;

-- ============================================================================
-- Signer Module Reset
-- ============================================================================
-- Original: 20251214000001_AddKeyManagementSchema.sql
-- New: 001_initial_schema (compacted)

DELETE FROM signer.schema_migrations
WHERE migration_name IN (
    '20251214000001_AddKeyManagementSchema.sql'
);

INSERT INTO signer.schema_migrations (migration_name, category, checksum, applied_at)
VALUES ('001_initial_schema.sql', 'startup', 'compacted_1.0.0', NOW())
ON CONFLICT (migration_name) DO NOTHING;

-- ============================================================================
-- Signals Module Reset
-- ============================================================================
-- Original: V0000_001__extensions.sql, V1102_001__unknowns_scoring_schema.sql,
--          V1105_001__deploy_refs_graph_metrics.sql, V3102_001__callgraph_relational_tables.sql
-- New: 001_initial_schema (compacted)

DELETE FROM signals.schema_migrations
WHERE migration_name IN (
    'V0000_001__extensions.sql',
    'V1102_001__unknowns_scoring_schema.sql',
    'V1105_001__deploy_refs_graph_metrics.sql',
    'V3102_001__callgraph_relational_tables.sql'
);

INSERT INTO signals.schema_migrations (migration_name, category, checksum, applied_at)
VALUES ('001_initial_schema.sql', 'startup', 'compacted_1.0.0', NOW())
ON CONFLICT (migration_name) DO NOTHING;

-- ============================================================================
-- Verification
-- ============================================================================
-- Display current migration status per module

DO $$
DECLARE
    v_module TEXT;
    v_count INT;
BEGIN
    FOR v_module IN SELECT unnest(ARRAY['authority', 'scheduler', 'scanner', 'policy', 'notify', 'concelier', 'proofchain', 'signer', 'signals']) LOOP
        EXECUTE format('SELECT COUNT(*) FROM %I.schema_migrations', v_module) INTO v_count;
        RAISE NOTICE '% module: % migrations registered', v_module, v_count;
    END LOOP;
END $$;

COMMIT;

-- ============================================================================
-- Post-Reset Notes
-- ============================================================================
-- After running this script:
-- 1. All modules should show exactly 1 migration registered
-- 2. The schema structure should be identical to a fresh 1.0.0 deployment
-- 3. Future migrations (002+) will apply normally
--
-- To verify manually:
-- SELECT * FROM authority.schema_migrations;
-- SELECT * FROM scheduler.schema_migrations;
-- SELECT * FROM scanner.schema_migrations;
-- SELECT * FROM policy.schema_migrations;
-- SELECT * FROM notify.schema_migrations;
-- SELECT * FROM concelier.schema_migrations;
-- SELECT * FROM proofchain.schema_migrations;
-- SELECT * FROM signer.schema_migrations;
-- SELECT * FROM signals.schema_migrations;
-- ============================================================================
