-- DEV Portal Migration
-- Features roadmap and workflow diagrams

-- Features/Roadmap table
CREATE TABLE IF NOT EXISTS dev_features (
    id SERIAL PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    version VARCHAR(50), -- V1, V2, V3, etc.
    status VARCHAR(50) DEFAULT 'planlagt', -- planlagt, i gang, færdig, sat på pause
    priority INTEGER DEFAULT 50,
    expected_date DATE,
    completed_date DATE,
    created_by_user_id INTEGER REFERENCES users(user_id) ON DELETE SET NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Brainstorm/Ideas table
CREATE TABLE IF NOT EXISTS dev_ideas (
    id SERIAL PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    category VARCHAR(100), -- feature, improvement, bugfix, etc.
    votes INTEGER DEFAULT 0,
    created_by_user_id INTEGER REFERENCES users(user_id) ON DELETE SET NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Workflow diagrams table (storing draw.io XML)
CREATE TABLE IF NOT EXISTS dev_workflows (
    id SERIAL PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    category VARCHAR(100), -- flowchart, process, system_diagram, etc.
    diagram_xml TEXT NOT NULL, -- draw.io XML format
    thumbnail_url TEXT, -- Preview image URL (optional)
    created_by_user_id INTEGER REFERENCES users(user_id) ON DELETE SET NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Indexes
CREATE INDEX IF NOT EXISTS idx_dev_features_version ON dev_features(version);
CREATE INDEX IF NOT EXISTS idx_dev_features_status ON dev_features(status);
CREATE INDEX IF NOT EXISTS idx_dev_workflows_category ON dev_workflows(category);

-- Update timestamp trigger for dev_features
CREATE OR REPLACE FUNCTION update_dev_features_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER dev_features_updated_at_trigger
BEFORE UPDATE ON dev_features
FOR EACH ROW
EXECUTE FUNCTION update_dev_features_updated_at();

-- Update timestamp trigger for dev_ideas
CREATE OR REPLACE FUNCTION update_dev_ideas_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER dev_ideas_updated_at_trigger
BEFORE UPDATE ON dev_ideas
FOR EACH ROW
EXECUTE FUNCTION update_dev_ideas_updated_at();

-- Update timestamp trigger for dev_workflows
CREATE OR REPLACE FUNCTION update_dev_workflows_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER dev_workflows_updated_at_trigger
BEFORE UPDATE ON dev_workflows
FOR EACH ROW
EXECUTE FUNCTION update_dev_workflows_updated_at();

-- Sample data
INSERT INTO dev_features (title, description, version, status, priority, expected_date) VALUES
('Dashboard Forbedringer', 'Live data og bedre statistik visualisering', 'V1', 'færdig', 90, '2025-12-01'),
('Global Søgning', 'Søg på tværs af kunder, kontakter, leverandører med CVR/telefon', 'V1', 'færdig', 95, '2025-12-06'),
('Settings & Brugerstyring', 'Konfiguration og user management', 'V1', 'færdig', 85, '2025-12-06'),
('vTiger Integration', 'Synkronisering med vTiger CRM', 'V2', 'planlagt', 80, '2026-01-15'),
('e-conomic Integration', 'Fakturering og økonomi sync', 'V2', 'planlagt', 75, '2026-02-01'),
('Rapport Generator', 'PDF rapporter for kunder', 'V3', 'planlagt', 60, '2026-03-01');

INSERT INTO dev_ideas (title, description, category, votes) VALUES
('Dark mode forbedringer', 'Bedre kontrast i dark mode', 'improvement', 5),
('Mobile app', 'Native iOS/Android app', 'feature', 12),
('AI Assistent', 'ChatGPT integration til kundesupport', 'feature', 8),
('Eksport til Excel', 'Eksporter alle lister til Excel', 'feature', 15);

COMMENT ON TABLE dev_features IS 'Development roadmap features with versioning and status tracking';
COMMENT ON TABLE dev_ideas IS 'Brainstorm and idea collection for future development';
COMMENT ON TABLE dev_workflows IS 'Workflow diagrams created with draw.io, stored as XML';
