-- ============================================================================
-- Migration: Unify common functions in public schema
-- Date: 2026-02-03
-- Task: ST-2.3
-- Issue: DUP-003 - update_updated_at() duplicated across schemas
-- ============================================================================
-- Purpose: Create a unified update_updated_at() function in the public schema
-- to replace duplicated functions in auth, education, financial, and
-- feature_flags schemas.
-- ============================================================================

-- ============================================================================
-- STEP 1: Create unified function in public schema
-- ============================================================================

CREATE OR REPLACE FUNCTION public.update_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

COMMENT ON FUNCTION public.update_updated_at() IS
'Unified trigger function for updated_at. Replaces schema-specific versions.
Issue: DUP-003 | Task: ST-2.3';

-- ============================================================================
-- STEP 2: Document deprecated functions
-- ============================================================================
-- The following schema-specific functions are now deprecated:
--
-- | Schema        | Function Name              | File Location                           |
-- |---------------|----------------------------|-----------------------------------------|
-- | auth          | update_updated_at()        | schemas/auth/functions/01-update_updated_at.sql |
-- | education     | update_updated_at_column() | schemas/education/functions/01-update_updated_at.sql |
-- | financial     | update_timestamp()         | schemas/financial/functions/03-triggers.sql |
-- | feature_flags | update_timestamp()         | schemas/feature_flags/tables/01-flags.sql |
--
-- These functions are NOT dropped in this migration to avoid breaking
-- existing triggers. Future migrations should:
--   1. Update triggers to use public.update_updated_at()
--   2. Drop the deprecated schema-specific functions
-- ============================================================================

-- ============================================================================
-- STEP 3: Example trigger update (DO NOT EXECUTE - FOR REFERENCE ONLY)
-- ============================================================================
-- To migrate a trigger from schema-specific to public function:
--
-- -- For auth.users table:
-- DROP TRIGGER IF EXISTS trigger_update_users_updated_at ON auth.users;
-- CREATE TRIGGER trigger_update_users_updated_at
--     BEFORE UPDATE ON auth.users
--     FOR EACH ROW
--     EXECUTE FUNCTION public.update_updated_at();
--
-- -- For education.courses table:
-- DROP TRIGGER IF EXISTS update_courses_updated_at ON education.courses;
-- CREATE TRIGGER update_courses_updated_at
--     BEFORE UPDATE ON education.courses
--     FOR EACH ROW
--     EXECUTE FUNCTION public.update_updated_at();
--
-- -- For financial.wallets table:
-- DROP TRIGGER IF EXISTS trigger_wallets_updated_at ON financial.wallets;
-- CREATE TRIGGER trigger_wallets_updated_at
--     BEFORE UPDATE ON financial.wallets
--     FOR EACH ROW
--     EXECUTE FUNCTION public.update_updated_at();
-- ============================================================================

-- ============================================================================
-- VERIFICATION QUERY
-- ============================================================================
-- Run this query to verify the function was created:
--
-- SELECT
--     n.nspname AS schema,
--     p.proname AS function_name,
--     d.description AS comment
-- FROM pg_proc p
-- JOIN pg_namespace n ON p.pronamespace = n.oid
-- LEFT JOIN pg_description d ON d.objoid = p.oid
-- WHERE p.proname = 'update_updated_at'
-- ORDER BY n.nspname;
-- ============================================================================

-- ============================================================================
-- ROLLBACK (if needed)
-- ============================================================================
-- DROP FUNCTION IF EXISTS public.update_updated_at();
-- ============================================================================
