-- 0007_vehicle_logs.sql — 保养 / 加油 / 充电三类用车记录
-- 三张表结构对称：(vehicle_id, log_date, odometer_km, location, total_cost, notes, created_at)
-- 业务差异字段单独存

CREATE TABLE IF NOT EXISTS maintenance_records (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    vehicle_id INTEGER NOT NULL,
    maint_date TEXT NOT NULL,
    odometer_km INTEGER,
    total_cost REAL NOT NULL DEFAULT 0,
    shop TEXT,
    items_json TEXT NOT NULL DEFAULT '[]',     -- [{name, cost, interval_km}, ...]
    next_due_date TEXT,
    next_due_km INTEGER,
    notes TEXT,
    created_at TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at TEXT NOT NULL DEFAULT (datetime('now')),
    FOREIGN KEY (vehicle_id) REFERENCES vehicles(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_maint_vehicle_date ON maintenance_records(vehicle_id, maint_date DESC);
CREATE INDEX IF NOT EXISTS idx_maint_date ON maintenance_records(maint_date DESC);

CREATE TABLE IF NOT EXISTS refuel_records (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    vehicle_id INTEGER NOT NULL,
    refuel_date TEXT NOT NULL,
    odometer_km INTEGER,
    liters REAL NOT NULL,
    price_per_liter REAL,
    total_cost REAL NOT NULL,
    fuel_type TEXT,                            -- 92 / 95 / 98 / 0#柴油 / 自定义
    is_full INTEGER NOT NULL DEFAULT 0,        -- 是否加满（计算油耗需要）
    station TEXT,
    notes TEXT,
    created_at TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at TEXT NOT NULL DEFAULT (datetime('now')),
    FOREIGN KEY (vehicle_id) REFERENCES vehicles(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_refuel_vehicle_date ON refuel_records(vehicle_id, refuel_date DESC);
CREATE INDEX IF NOT EXISTS idx_refuel_date ON refuel_records(refuel_date DESC);

CREATE TABLE IF NOT EXISTS charging_records (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    vehicle_id INTEGER NOT NULL,
    charge_date TEXT NOT NULL,
    odometer_km INTEGER,
    kwh REAL NOT NULL,
    price_per_kwh REAL,
    total_cost REAL NOT NULL,
    charge_type TEXT,                          -- slow (慢充/交流) / fast (快充/直流) / home (家充) / public (公共桩)
    start_soc INTEGER,                         -- 起始电量 %
    end_soc INTEGER,                           -- 结束电量 %
    station TEXT,
    notes TEXT,
    created_at TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at TEXT NOT NULL DEFAULT (datetime('now')),
    FOREIGN KEY (vehicle_id) REFERENCES vehicles(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_charging_vehicle_date ON charging_records(vehicle_id, charge_date DESC);
CREATE INDEX IF NOT EXISTS idx_charging_date ON charging_records(charge_date DESC);

-- 一张视图方便首页拿"最近 30 天每类最新 5 条"
DROP VIEW IF EXISTS v_recent_logs;
CREATE VIEW v_recent_logs AS
SELECT 'maintenance' AS log_type, id, vehicle_id, maint_date AS log_date,
       total_cost, odometer_km, shop AS location
FROM maintenance_records
UNION ALL
SELECT 'refuel' AS log_type, id, vehicle_id, refuel_date AS log_date,
       total_cost, odometer_km, station AS location
FROM refuel_records
UNION ALL
SELECT 'charging' AS log_type, id, vehicle_id, charge_date AS log_date,
       total_cost, odometer_km, station AS location
FROM charging_records;
