-- Расширение товаров, скриншоты, статистика посещений

ALTER TABLE products ADD COLUMN IF NOT EXISTS details TEXT NOT NULL DEFAULT '';
ALTER TABLE products ADD COLUMN IF NOT EXISTS is_active BOOLEAN NOT NULL DEFAULT true;
ALTER TABLE products ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW();

CREATE TABLE IF NOT EXISTS product_images (
    id          SERIAL PRIMARY KEY,
    product_id  INT NOT NULL REFERENCES products(id) ON DELETE CASCADE,
    file_path   VARCHAR(512) NOT NULL,
    is_primary  BOOLEAN NOT NULL DEFAULT false,
    sort_order  INT NOT NULL DEFAULT 0,
    created_at  TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_product_images_product ON product_images (product_id);

CREATE TABLE IF NOT EXISTS site_stats (
    id           INT PRIMARY KEY DEFAULT 1 CHECK (id = 1),
    total_visits BIGINT NOT NULL DEFAULT 0,
    today_visits INT NOT NULL DEFAULT 0,
    visit_date   DATE NOT NULL DEFAULT CURRENT_DATE
);

INSERT INTO site_stats (id) VALUES (1) ON CONFLICT (id) DO NOTHING;
