-- =====================================================
-- 001_initial.sql — начальная схема БД для лендинга
-- =====================================================

CREATE TABLE IF NOT EXISTS admin_users (
    id          SERIAL PRIMARY KEY,
    login       VARCHAR(64) UNIQUE NOT NULL,
    password_hash TEXT NOT NULL,
    created_at  TIMESTAMPTZ NOT NULL DEFAULT now(),
    last_login  TIMESTAMPTZ
);

CREATE TABLE IF NOT EXISTS sections (
    id           SERIAL PRIMARY KEY,
    key          VARCHAR(64) UNIQUE NOT NULL,
    title        TEXT NOT NULL,
    content_json JSONB NOT NULL DEFAULT '{}'::jsonb,
    is_active    BOOLEAN NOT NULL DEFAULT true,
    sort_order   INTEGER NOT NULL DEFAULT 0,
    updated_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS products (
    id           SERIAL PRIMARY KEY,
    slug         VARCHAR(64) UNIQUE NOT NULL,
    title        VARCHAR(128) NOT NULL,
    subtitle     VARCHAR(256),
    description  TEXT NOT NULL,
    status       VARCHAR(32) NOT NULL DEFAULT 'production',
    audience     VARCHAR(32),
    icon_key     VARCHAR(64),
    tags         JSONB NOT NULL DEFAULT '[]'::jsonb,
    is_published BOOLEAN NOT NULL DEFAULT true,
    sort_order   INTEGER NOT NULL DEFAULT 0,
    created_at   TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS approach_items (
    id          SERIAL PRIMARY KEY,
    title       VARCHAR(128) NOT NULL,
    description TEXT NOT NULL,
    icon_key    VARCHAR(64),
    sort_order  INTEGER NOT NULL DEFAULT 0,
    is_active   BOOLEAN NOT NULL DEFAULT true
);

CREATE TABLE IF NOT EXISTS settings (
    key         VARCHAR(64) PRIMARY KEY,
    value       TEXT NOT NULL,
    updated_at  TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_products_published_sorted
    ON products(is_published, sort_order) WHERE is_published = true;
CREATE INDEX IF NOT EXISTS idx_sections_active_sorted
    ON sections(is_active, sort_order) WHERE is_active = true;
CREATE INDEX IF NOT EXISTS idx_approach_active_sorted
    ON approach_items(is_active, sort_order) WHERE is_active = true;
