-- =====================================================================
-- WebGIS Citizen Participation Portal
-- Migration: 002_add_votes_index.sql
-- Description: Adds missing Index on votes.contribution_id for fast
--              Vote Lookups per Contribution.
-- =====================================================================


-- ---------------------------------------------------------------------
-- Block 1: Index for fast Queries
-- The UNIQUE Constraint on contribution_id and voter_name creates a
-- composite Index, but Queries filtering only by contribution_id
-- cannot use it efficiently. This single-column Index covers that Case.
-- ---------------------------------------------------------------------
CREATE INDEX votes_contribution_idx ON votes (contribution_id);


-- =====================================================================
-- ToDo's for future Migrations
-- =====================================================================
--
-- 1. Categories Table
--    Create a "categories" Table with municipality_id, slug, label,
--    icon (FontAwesome), color, and sort_order. Replace the free-text
--    "category" Column in Contributions with a Foreign Key Reference.
--    This prevents Typos and inconsistent Category Names, and allows
--    each Municipality to define its own Set of Categories.
--
-- 2. Soft Delete
--    Add "deleted_at TIMESTAMPTZ DEFAULT NULL" to Contributions.
--    Instead of DELETE, set deleted_at = NOW(). Filter all Queries
--    with "WHERE deleted_at IS NULL". Allows Moderation Audit Trail
--    and accidental Deletion Recovery.
--
-- 3. Audit Log
--    Create an "audit_log" Table recording who changed what and when.
--    Columns: audit_id, table_name, record_id, action (insert/update/
--    delete), changed_by, old_values (JSONB), new_values (JSONB),
--    created_at. Populate via Triggers on Contributions and Votes.
--
-- 4. Geometry Validation
--    Add CHECK Constraint "ST_IsValid(geom)" on Contributions, or
--    validate in the API Layer before Insert. Prevents self-crossing
--    Polygons and other invalid Geometries.
--
-- =====================================================================
-- End of migration 002_add_votes_index.sql
-- =====================================================================