-- v3.8 — Supplier Catalog tables
-- Adds supplier_catalog, supplier_catalog_compat, and supplier_catalog_interchange
-- to support multi-supplier parts injection into the vehicle catalog.

CREATE TABLE IF NOT EXISTS supplier_catalog (
    id              SERIAL PRIMARY KEY,
    tenant_id       INTEGER NOT NULL,
    supplier_name   VARCHAR(255) NOT NULL,
    sku             VARCHAR(255) NOT NULL,
    name            VARCHAR(500) NOT NULL,
    category        VARCHAR(255),
    description     TEXT,
    image_url       TEXT,
    is_active       BOOLEAN DEFAULT TRUE,
    created_at      TIMESTAMPTZ DEFAULT NOW()
);

CREATE UNIQUE INDEX IF NOT EXISTS supplier_catalog_tenant_id_supplier_name_sku_category_key
    ON supplier_catalog (tenant_id, supplier_name, sku, category);

CREATE INDEX IF NOT EXISTS idx_sc_supplier
    ON supplier_catalog (tenant_id, supplier_name, is_active);

CREATE INDEX IF NOT EXISTS idx_sc_sku
    ON supplier_catalog (tenant_id, sku, category);


CREATE TABLE IF NOT EXISTS supplier_catalog_compat (
    id                  SERIAL PRIMARY KEY,
    catalog_id          INTEGER NOT NULL REFERENCES supplier_catalog(id) ON DELETE CASCADE,
    make                VARCHAR(255),
    model               VARCHAR(255),
    year                INTEGER,
    engine              VARCHAR(255),
    engine_code         VARCHAR(255),
    model_year_engine_id INTEGER,
    source              VARCHAR(50) DEFAULT 'import',
    created_at          TIMESTAMPTZ DEFAULT NOW()
);

CREATE UNIQUE INDEX IF NOT EXISTS supplier_catalog_compat_catalog_id_make_model_year_engine_key
    ON supplier_catalog_compat (catalog_id, make, model, year, engine);

CREATE INDEX IF NOT EXISTS idx_scc_catalog
    ON supplier_catalog_compat (catalog_id);

CREATE INDEX IF NOT EXISTS idx_scc_vehicle
    ON supplier_catalog_compat (make, model, year);

CREATE INDEX IF NOT EXISTS idx_scc_mye
    ON supplier_catalog_compat (model_year_engine_id);


CREATE TABLE IF NOT EXISTS supplier_catalog_interchange (
    id              SERIAL PRIMARY KEY,
    catalog_id      INTEGER NOT NULL REFERENCES supplier_catalog(id) ON DELETE CASCADE,
    brand           VARCHAR(255),
    part_number     VARCHAR(255),
    created_at      TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_sci_catalog
    ON supplier_catalog_interchange (catalog_id);
