-- ============================================================
-- Schema completo per il database "rules"
-- ============================================================

-- ============ DATA / BROWSER ============

CREATE TABLE IF NOT EXISTS dataread (
    id          CHAR(8) PRIMARY KEY,
    version     VARCHAR(20) NOT NULL,
    tags        TEXT[] DEFAULT '{}',
    active      BOOLEAN NOT NULL DEFAULT false,
    archived    BOOLEAN NOT NULL DEFAULT false,
    created_at  TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at  TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS datareaditems (
    id         BIGSERIAL PRIMARY KEY,
    rule_id    CHAR(8) NOT NULL REFERENCES dataread(id) ON DELETE CASCADE,
    category   VARCHAR(50) NOT NULL,
    path       VARCHAR(200) NOT NULL,
    unit       VARCHAR(20),
    enabled    BOOLEAN NOT NULL DEFAULT true,
    UNIQUE(rule_id, path)
);

CREATE INDEX IF NOT EXISTS idx_datareaditems_rule ON datareaditems(rule_id);

-- ============ WEATHER (current, ogni 5 min) ============

CREATE TABLE IF NOT EXISTS weather (
    id          CHAR(8) PRIMARY KEY,
    version     VARCHAR(20) NOT NULL,
    description TEXT,
    tags        TEXT[] DEFAULT '{}',
    active      BOOLEAN NOT NULL DEFAULT false,
    archived    BOOLEAN NOT NULL DEFAULT false,
    created_at  TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at  TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS weatheritems (
    id          BIGSERIAL PRIMARY KEY,
    rule_id     CHAR(8) NOT NULL REFERENCES weather(id) ON DELETE CASCADE,
    group_name  VARCHAR(50) NOT NULL,
    ref         VARCHAR(50) NOT NULL,
    name        VARCHAR(50) NOT NULL,
    unit        VARCHAR(20) NOT NULL,
    enabled     BOOLEAN NOT NULL DEFAULT true,
    UNIQUE(rule_id, ref)
);

CREATE INDEX IF NOT EXISTS idx_weatheritems_rule ON weatheritems(rule_id);

-- ============ LATERFORECASTS (hourly 7gg, ogni 1 ora) ============

CREATE TABLE IF NOT EXISTS laterforecasts (
    id          CHAR(8) PRIMARY KEY,
    version     VARCHAR(20) NOT NULL,
    description TEXT,
    tags        TEXT[] DEFAULT '{}',
    active      BOOLEAN NOT NULL DEFAULT false,
    archived    BOOLEAN NOT NULL DEFAULT false,
    created_at  TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at  TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS laterforecastitems (
    id          BIGSERIAL PRIMARY KEY,
    rule_id     CHAR(8) NOT NULL REFERENCES laterforecasts(id) ON DELETE CASCADE,
    group_name  VARCHAR(50) NOT NULL,
    ref         VARCHAR(50) NOT NULL,
    name        VARCHAR(50) NOT NULL,
    unit        VARCHAR(20) NOT NULL,
    enabled     BOOLEAN NOT NULL DEFAULT true,
    UNIQUE(rule_id, ref)
);

CREATE INDEX IF NOT EXISTS idx_laterforecastitems_rule ON laterforecastitems(rule_id);

-- ============ LOGS ============

CREATE TABLE IF NOT EXISTS logs (
    id              CHAR(8) PRIMARY KEY,
    version         VARCHAR(20) NOT NULL,
    description     TEXT,
    tags            TEXT[] DEFAULT '{}',
    active          BOOLEAN NOT NULL DEFAULT false,
    archived        BOOLEAN NOT NULL DEFAULT false,
    browser_rule_id CHAR(8) REFERENCES dataread(id),
    created_at      TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at      TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS logitems (
    id          BIGSERIAL PRIMARY KEY,
    rule_id     CHAR(8) NOT NULL REFERENCES logs(id) ON DELETE CASCADE,
    path        VARCHAR(200) NOT NULL,
    ref         VARCHAR(4) NOT NULL,
    unit        VARCHAR(20) NOT NULL,
    measurement VARCHAR(50) NOT NULL,
    enabled     BOOLEAN NOT NULL DEFAULT true,
    UNIQUE(rule_id, ref),
    UNIQUE(rule_id, path),
    CHECK (LENGTH(ref) <= 4)
);

CREATE INDEX IF NOT EXISTS idx_logitems_rule ON logitems(rule_id);

-- ============ KIOSK ============

CREATE TABLE IF NOT EXISTS kiosktemplates (
    id          CHAR(8) PRIMARY KEY NOT NULL,
    name        VARCHAR(50) NOT NULL,
    tags        TEXT[] DEFAULT '{}',
    active      BOOLEAN NOT NULL DEFAULT false,
    archived    BOOLEAN NOT NULL DEFAULT false,
    created_at  TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at  TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS kioskelements (
    id          BIGSERIAL PRIMARY KEY,
    template_id CHAR(8) NOT NULL REFERENCES kiosktemplates(id) ON DELETE CASCADE,
    font        INT NOT NULL,
    label       VARCHAR(100) NOT NULL,
    x           INT NOT NULL,
    y           INT NOT NULL,
    width       INT NOT NULL,
    height      INT NOT NULL,
    color       VARCHAR(20) NOT NULL,
    UNIQUE(template_id)
);

CREATE INDEX IF NOT EXISTS idx_kioskelements_template ON kioskelements(template_id);

-- ============ VINCOLI: una sola rule attiva per tipo ============

CREATE UNIQUE INDEX IF NOT EXISTS idx_one_active_dataread
    ON dataread(active) WHERE active = true AND archived = false;

CREATE UNIQUE INDEX IF NOT EXISTS idx_one_active_weather
    ON weather(active) WHERE active = true AND archived = false;

CREATE UNIQUE INDEX IF NOT EXISTS idx_one_active_laterforecasts
    ON laterforecasts(active) WHERE active = true AND archived = false;

CREATE UNIQUE INDEX IF NOT EXISTS idx_one_active_logs
    ON logs(active) WHERE active = true AND archived = false;

-- ============ FIX per schema esistente ============

DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'datareaditems' AND column_name = 'enables'
    ) THEN
        ALTER TABLE datareaditems RENAME COLUMN enables TO enabled;
    END IF;
END $$;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'weather' AND column_name = 'description'
    ) THEN
        ALTER TABLE weather ADD COLUMN description TEXT;
    END IF;
END $$;
