-- Extend 'lots' table
ALTER TABLE lots
    ADD COLUMN starting_bid DECIMAL(12, 2);
ALTER TABLE lots
    ADD COLUMN estimated_min DECIMAL(12, 2);
ALTER TABLE lots
    ADD COLUMN estimated_max DECIMAL(12, 2);
ALTER TABLE lots
    ADD COLUMN reserve_price DECIMAL(12, 2);
ALTER TABLE lots
    ADD COLUMN reserve_met BOOLEAN DEFAULT FALSE;
ALTER TABLE lots
    ADD COLUMN bid_increment DECIMAL(12, 2);
ALTER TABLE lots
    ADD COLUMN watch_count INTEGER DEFAULT 0;
ALTER TABLE lots
    ADD COLUMN view_count INTEGER DEFAULT 0;
ALTER TABLE lots
    ADD COLUMN first_bid_time TEXT;
ALTER TABLE lots
    ADD COLUMN last_bid_time TEXT;
ALTER TABLE lots
    ADD COLUMN bid_velocity DECIMAL(5, 2);
-- bids per hour

-- New table: bid history (CRITICAL)
CREATE TABLE bid_history
(
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    lot_id     TEXT REFERENCES lots (lot_id),
    bid_amount DECIMAL(12, 2) NOT NULL,
    bid_time   TEXT           NOT NULL,
    is_winning BOOLEAN DEFAULT FALSE,
    is_autobid BOOLEAN DEFAULT FALSE,
    bidder_id  TEXT, -- anonymized
    created_at TEXT    DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_bid_history_lot_time ON bid_history (lot_id, bid_time);
-- Extend 'lots' table
ALTER TABLE lots
    ADD COLUMN condition_score DECIMAL(3, 2); -- 0.00-10.00
ALTER TABLE lots
    ADD COLUMN condition_description TEXT;
ALTER TABLE lots
    ADD COLUMN year_manufactured INTEGER;
ALTER TABLE lots
    ADD COLUMN serial_number TEXT;
ALTER TABLE lots
    ADD COLUMN originality_score DECIMAL(3, 2); -- % original parts
ALTER TABLE lots
    ADD COLUMN provenance TEXT;
ALTER TABLE lots
    ADD COLUMN comparable_lot_ids TEXT;
-- JSON array

-- New table: comparable sales
CREATE TABLE comparable_sales
(
    id                       INTEGER PRIMARY KEY AUTOINCREMENT,
    lot_id                   TEXT REFERENCES lots (lot_id),
    comparable_lot_id        TEXT,
    similarity_score         DECIMAL(3, 2), -- 0.00-1.00
    price_difference_percent DECIMAL(5, 2),
    created_at               TEXT DEFAULT CURRENT_TIMESTAMP
);

-- New table: market indices
CREATE TABLE market_indices
(
    id                INTEGER PRIMARY KEY AUTOINCREMENT,
    category          TEXT NOT NULL,
    manufacturer      TEXT,
    avg_price         DECIMAL(12, 2),
    median_price      DECIMAL(12, 2),
    price_change_30d  DECIMAL(5, 2),
    volume_change_30d DECIMAL(5, 2),
    calculated_at     TEXT DEFAULT CURRENT_TIMESTAMP
);
-- Extend 'auctions' table
ALTER TABLE auctions
    ADD COLUMN auction_house TEXT;
ALTER TABLE auctions
    ADD COLUMN auction_house_rating DECIMAL(3, 2);
ALTER TABLE auctions
    ADD COLUMN buyers_premium_percent DECIMAL(5, 2);
ALTER TABLE auctions
    ADD COLUMN payment_methods TEXT; -- JSON
ALTER TABLE auctions
    ADD COLUMN shipping_cost_min DECIMAL(12, 2);
ALTER TABLE auctions
    ADD COLUMN shipping_cost_max DECIMAL(12, 2);
ALTER TABLE auctions
    ADD COLUMN seller_verified BOOLEAN DEFAULT FALSE;

-- New table: auction performance metrics
CREATE TABLE auction_metrics
(
    id                     INTEGER PRIMARY KEY AUTOINCREMENT,
    auction_id             TEXT REFERENCES auctions (auction_id),
    sell_through_rate      DECIMAL(5, 2),
    avg_hammer_vs_estimate DECIMAL(5, 2),
    total_hammer_price     DECIMAL(15, 2),
    total_starting_price   DECIMAL(15, 2),
    calculated_at          TEXT DEFAULT CURRENT_TIMESTAMP
);

-- New table: seasonal trends
CREATE TABLE seasonal_trends
(
    id                   INTEGER PRIMARY KEY AUTOINCREMENT,
    category             TEXT    NOT NULL,
    month                INTEGER NOT NULL,
    avg_price_multiplier DECIMAL(4, 2), -- vs annual avg
    volume_multiplier    DECIMAL(4, 2),
    PRIMARY KEY (category, month)
);
-- New table: external market data
CREATE TABLE external_market_data
(
    id                  INTEGER PRIMARY KEY AUTOINCREMENT,
    category            TEXT NOT NULL,
    manufacturer        TEXT,
    model               TEXT,
    dealer_avg_price    DECIMAL(12, 2),
    retail_avg_price    DECIMAL(12, 2),
    wholesale_avg_price DECIMAL(12, 2),
    source              TEXT,
    fetched_at          TEXT DEFAULT CURRENT_TIMESTAMP
);

-- New table: image analysis results
CREATE TABLE image_analysis
(
    id                   INTEGER PRIMARY KEY AUTOINCREMENT,
    image_id             INTEGER REFERENCES images (id),
    damage_detected      BOOLEAN,
    damage_severity      DECIMAL(3, 2),
    wear_level           TEXT CHECK (wear_level IN ('EXCELLENT', 'GOOD', 'FAIR', 'POOR')),
    estimated_hours_used INTEGER,
    ai_confidence        DECIMAL(3, 2)
);

-- New table: economic indicators
CREATE TABLE economic_indicators
(
    id                INTEGER PRIMARY KEY AUTOINCREMENT,
    indicator_date    TEXT NOT NULL,
    currency          TEXT NOT NULL,
    exchange_rate     DECIMAL(10, 4),
    inflation_rate    DECIMAL(5, 2),
    market_volatility DECIMAL(5, 2)
);
