-- Test Module Module - Initial Migration
-- Opret basis tabeller for template module

-- Items tabel (eksempel)
CREATE TABLE IF NOT EXISTS test_module_items (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    description TEXT,
    active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Index for performance
CREATE INDEX IF NOT EXISTS idx_test_module_items_active ON test_module_items(active);
CREATE INDEX IF NOT EXISTS idx_test_module_items_created ON test_module_items(created_at DESC);

-- Trigger for updated_at
CREATE OR REPLACE FUNCTION update_test_module_items_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigger_test_module_items_updated_at
BEFORE UPDATE ON test_module_items
FOR EACH ROW
EXECUTE FUNCTION update_test_module_items_updated_at();

-- Indsæt test data (optional)
INSERT INTO test_module_items (name, description) 
VALUES 
    ('Test Item 1', 'This is a test item from template module'),
    ('Test Item 2', 'Another test item')
ON CONFLICT DO NOTHING;
