-- Migration: Add approval columns to tmodule_times if they don't exist
-- Version: 1.3.83+
-- Date: 2026-01-02

-- Add approved_hours column if it doesn't exist
DO $$ 
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'tmodule_times' AND column_name = 'approved_hours'
    ) THEN
        ALTER TABLE tmodule_times ADD COLUMN approved_hours DECIMAL(10,2);
        RAISE NOTICE 'Added column approved_hours';
    END IF;
END $$;

-- Add rounded_to column if it doesn't exist
DO $$ 
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'tmodule_times' AND column_name = 'rounded_to'
    ) THEN
        ALTER TABLE tmodule_times ADD COLUMN rounded_to DECIMAL(10,2);
        RAISE NOTICE 'Added column rounded_to';
    END IF;
END $$;

-- Add approval_note column if it doesn't exist
DO $$ 
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'tmodule_times' AND column_name = 'approval_note'
    ) THEN
        ALTER TABLE tmodule_times ADD COLUMN approval_note TEXT;
        RAISE NOTICE 'Added column approval_note';
    END IF;
END $$;

-- Add billable column if it doesn't exist
DO $$ 
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'tmodule_times' AND column_name = 'billable'
    ) THEN
        ALTER TABLE tmodule_times ADD COLUMN billable BOOLEAN DEFAULT TRUE;
        RAISE NOTICE 'Added column billable';
    END IF;
END $$;

-- Add is_travel column if it doesn't exist
DO $$ 
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'tmodule_times' AND column_name = 'is_travel'
    ) THEN
        ALTER TABLE tmodule_times ADD COLUMN is_travel BOOLEAN DEFAULT FALSE;
        RAISE NOTICE 'Added column is_travel';
    END IF;
END $$;

-- Add approved_at column if it doesn't exist
DO $$ 
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'tmodule_times' AND column_name = 'approved_at'
    ) THEN
        ALTER TABLE tmodule_times ADD COLUMN approved_at TIMESTAMP;
        RAISE NOTICE 'Added column approved_at';
    END IF;
END $$;

-- Add approved_by column if it doesn't exist
DO $$ 
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'tmodule_times' AND column_name = 'approved_by'
    ) THEN
        ALTER TABLE tmodule_times ADD COLUMN approved_by INTEGER;
        RAISE NOTICE 'Added column approved_by';
    END IF;
END $$;

-- Create index on status if it doesn't exist
DO $$ 
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_indexes 
        WHERE tablename = 'tmodule_times' AND indexname = 'idx_tmodule_times_status'
    ) THEN
        CREATE INDEX idx_tmodule_times_status ON tmodule_times(status);
        RAISE NOTICE 'Created index idx_tmodule_times_status';
    END IF;
END $$;

-- Create index on approved_at if it doesn't exist
DO $$ 
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_indexes 
        WHERE tablename = 'tmodule_times' AND indexname = 'idx_tmodule_times_approved_at'
    ) THEN
        CREATE INDEX idx_tmodule_times_approved_at ON tmodule_times(approved_at);
        RAISE NOTICE 'Created index idx_tmodule_times_approved_at';
    END IF;
END $$;
