-- Tag System Migration
-- Central tagging system for all entities in BMC Hub

-- Main tags table
CREATE TABLE IF NOT EXISTS tags (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    type VARCHAR(20) NOT NULL CHECK (type IN ('workflow', 'status', 'category', 'priority', 'billing')),
    description TEXT,
    color VARCHAR(7) NOT NULL, -- Hex color code
    icon VARCHAR(50), -- Optional icon name
    is_active BOOLEAN DEFAULT true,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(name, type)
);

-- Polymorphic entity tagging
CREATE TABLE IF NOT EXISTS entity_tags (
    id SERIAL PRIMARY KEY,
    entity_type VARCHAR(50) NOT NULL, -- 'ticket', 'customer', 'time_entry', 'contact', etc.
    entity_id INTEGER NOT NULL,
    tag_id INTEGER NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
    tagged_by INTEGER, -- User who added the tag
    tagged_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(entity_type, entity_id, tag_id)
);

-- Workflow trigger configurations
CREATE TABLE IF NOT EXISTS tag_workflows (
    id SERIAL PRIMARY KEY,
    tag_id INTEGER NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
    trigger_event VARCHAR(50) NOT NULL, -- 'on_add', 'on_remove'
    action_type VARCHAR(50) NOT NULL, -- 'send_email', 'update_status', 'assign_user', 'create_task'
    action_config JSONB, -- Flexible config for different actions
    is_active BOOLEAN DEFAULT true,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Indexes for performance
CREATE INDEX idx_entity_tags_entity ON entity_tags(entity_type, entity_id);
CREATE INDEX idx_entity_tags_tag ON entity_tags(tag_id);
CREATE INDEX idx_tags_type ON tags(type);
CREATE INDEX idx_tags_active ON tags(is_active) WHERE is_active = true;

-- Insert default tags
INSERT INTO tags (name, type, description, color) VALUES
-- Workflow tags (orange)
('Firstline', 'workflow', 'Skal håndteres af firstline support', '#ff6b35'),
('Fakturerbar', 'workflow', 'Tid skal faktureres til kunde', '#ff6b35'),
('Escalate', 'workflow', 'Skal eskaleres til senior tekniker', '#ff6b35'),

-- Status tags (yellow)
('Afventer kunde', 'status', 'Venter på svar fra kunde', '#ffd700'),
('Løst', 'status', 'Sagen er løst', '#28a745'),
('Parkeret', 'status', 'Sat på hold', '#6c757d'),
('I gang', 'status', 'Bliver arbejdet på', '#17a2b8'),

-- Category tags (blue)
('Netværk', 'category', 'Netværksrelateret problem', '#0f4c75'),
('Hardware', 'category', 'Hardware problem', '#0f4c75'),
('Software', 'category', 'Software problem', '#0f4c75'),
('Printerfejl', 'category', 'Printer relateret', '#0f4c75'),
('Mail', 'category', 'Email relateret', '#0f4c75'),

-- Priority tags (red to green)
('Kritisk', 'priority', 'Skal løses med det samme', '#dc3545'),
('Høj', 'priority', 'Høj prioritet', '#fd7e14'),
('Normal', 'priority', 'Normal prioritet', '#ffc107'),
('Lav', 'priority', 'Kan vente', '#28a745'),

-- Billing tags (green)
('SLA-inkluderet', 'billing', 'Dækket af SLA', '#2d6a4f'),
('Gratis', 'billing', 'Ingen fakturering', '#2d6a4f'),
('Ekstra arbejde', 'billing', 'Skal faktureres ekstra', '#2d6a4f');

COMMENT ON TABLE tags IS 'Central tag definitions used across all entities';
COMMENT ON TABLE entity_tags IS 'Polymorphic association between tags and any entity';
COMMENT ON TABLE tag_workflows IS 'Automation triggers based on tags';
