-- FloorVisualizer Database Schema
-- Generated from PostgreSQL

-- Users
CREATE TABLE IF NOT EXISTS users (
    id          TEXT PRIMARY KEY,
    username    TEXT NOT NULL UNIQUE,
    password    TEXT NOT NULL DEFAULT '',
    name        TEXT NOT NULL DEFAULT '',
    avatar_url  TEXT NOT NULL DEFAULT '',
    created_at  TIMESTAMPTZ DEFAULT now(),
    updated_at  TIMESTAMPTZ DEFAULT now()
);

-- Products
CREATE TABLE IF NOT EXISTS products (
    id                   SERIAL PRIMARY KEY,
    brand                TEXT DEFAULT '',
    group_name           TEXT DEFAULT '',
    sku                  TEXT DEFAULT '',
    series_name          TEXT DEFAULT '',
    style_name           TEXT DEFAULT '',
    category             TEXT DEFAULT '',
    material             TEXT DEFAULT '',
    color_tone           TEXT DEFAULT '',
    finish               TEXT DEFAULT '',
    price_per_sqft       NUMERIC(10,2) DEFAULT 0,
    price_tier           TEXT DEFAULT '',
    price_source         TEXT DEFAULT '',
    main_image_url       TEXT DEFAULT '',
    room_image_url       TEXT DEFAULT '',
    source_url           TEXT DEFAULT '',
    description          TEXT DEFAULT '',
    status               TEXT DEFAULT '',
    coverage_sqft_per_box NUMERIC(10,2) DEFAULT 0,
    wood_species         TEXT DEFAULT '',
    size_label           TEXT DEFAULT '',
    width_in             NUMERIC(10,2),
    length_in            NUMERIC(10,2)
);
CREATE INDEX IF NOT EXISTS idx_p_sku ON products(sku);
CREATE INDEX IF NOT EXISTS idx_p_cat ON products(category);
CREATE INDEX IF NOT EXISTS idx_p_brand ON products(brand);
CREATE INDEX IF NOT EXISTS idx_p_style ON products(style_name);

-- Articles
CREATE TABLE IF NOT EXISTS articles (
    id          SERIAL PRIMARY KEY,
    title       TEXT NOT NULL DEFAULT '',
    summary     TEXT NOT NULL DEFAULT '',
    content     TEXT NOT NULL DEFAULT '',
    cover_url   TEXT NOT NULL DEFAULT '',
    image_urls  JSONB NOT NULL DEFAULT '[]',
    created_at  TIMESTAMPTZ DEFAULT now()
);

-- Favorites
CREATE TABLE IF NOT EXISTS favorites (
    id              SERIAL PRIMARY KEY,
    user_id         TEXT NOT NULL,
    sku             TEXT NOT NULL,
    brand           TEXT DEFAULT '',
    series_name     TEXT DEFAULT '',
    style_name      TEXT DEFAULT '',
    main_image_url  TEXT DEFAULT '',
    category        TEXT DEFAULT '',
    material        TEXT DEFAULT '',
    color_tone      TEXT DEFAULT '',
    price_per_sqft  NUMERIC(10,2) DEFAULT 0,
    created_at      TIMESTAMPTZ DEFAULT now(),
    UNIQUE(user_id, sku)
);
CREATE INDEX IF NOT EXISTS idx_fav_user ON favorites(user_id);

-- Projects
CREATE TABLE IF NOT EXISTS projects (
    id                   TEXT PRIMARY KEY,
    user_id              TEXT NOT NULL,
    name                 TEXT NOT NULL DEFAULT 'Untitled',
    original_image_url   TEXT DEFAULT '',
    generated_image_url  TEXT DEFAULT '',
    floor_style          TEXT DEFAULT '',
    pattern              TEXT DEFAULT '',
    room_type            TEXT DEFAULT '',
    created_at           TIMESTAMPTZ DEFAULT now(),
    updated_at           TIMESTAMPTZ DEFAULT now()
);

-- Brand Views
CREATE TABLE IF NOT EXISTS user_brand_views (
    id         SERIAL PRIMARY KEY,
    user_id    TEXT NOT NULL,
    brand      TEXT NOT NULL,
    created_at TIMESTAMPTZ DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_ubv_user ON user_brand_views(user_id, brand);
CREATE INDEX IF NOT EXISTS idx_ubv_time ON user_brand_views(user_id, created_at);
