PRAGMA foreign_keys = OFF;

CREATE TABLE service_groups (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    type TEXT NOT NULL DEFAULT 'custom',
    icon TEXT,
    domain TEXT,
    enabled INTEGER NOT NULL DEFAULT 1,
    created_at TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);

ALTER TABLE services ADD COLUMN service_group_id INTEGER REFERENCES service_groups(id) ON DELETE SET NULL;
ALTER TABLE services ADD COLUMN subdomain TEXT;
ALTER TABLE services ADD COLUMN enabled INTEGER NOT NULL DEFAULT 0;

UPDATE services SET subdomain = slug WHERE subdomain IS NULL;

INSERT INTO service_groups (name, type, enabled) VALUES
    ('VPN', 'vpn', 1),
    ('Сеть', 'network', 1),
    ('Интернет', 'internet', 1);

UPDATE services
SET service_group_id = (SELECT id FROM service_groups WHERE type = 'vpn' LIMIT 1)
WHERE slug IN ('vpn-panel', 'vpn-node');

PRAGMA foreign_keys = ON;
