-- ============================================================================
-- OrbiQuant IA - Trading Platform
-- File: ddl/00-global-functions.sql
-- Description: Global Functions - Used across all schemas
-- Created: 2026-02-03
-- Task: ST-2.3 - Unify common functions in public schema
-- Issue: DUP-003
-- ============================================================================
-- These functions are created in the public schema for reuse across all
-- domain schemas (auth, trading, financial, education, etc.)
-- ============================================================================

-- ============================================================================
-- FUNCTION: public.update_updated_at()
-- Purpose: Automatically update the updated_at column on row modification
-- Usage: Create trigger on tables with updated_at column
-- ============================================================================
-- Replaces duplicated functions:
--   - auth.update_updated_at()
--   - education.update_updated_at_column()
--   - financial.update_timestamp()
--   - feature_flags.update_timestamp()
-- ============================================================================

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
'Trigger function to auto-update updated_at timestamp. Use in BEFORE UPDATE triggers.
Unified function replacing schema-specific versions (auth, education, financial, feature_flags).
Migration: migrations/2026-02-03_unify_common_functions.sql | Issue: DUP-003';

-- ============================================================================
-- USAGE EXAMPLE
-- ============================================================================
-- CREATE TRIGGER trg_tablename_updated_at
--     BEFORE UPDATE ON schema.tablename
--     FOR EACH ROW
--     EXECUTE FUNCTION public.update_updated_at();
-- ============================================================================
