CREATE TABLE IF NOT EXISTS person (
    id SERIAL PRIMARY KEY,
    name VARCHAR(200) NOT NULL,
    phone VARCHAR(100)
);

CREATE TABLE IF NOT EXISTS vehicle (
    id SERIAL PRIMARY KEY,
    person_id INTEGER NOT NULL REFERENCES person(id) ON DELETE CASCADE,
    brand VARCHAR(120) NOT NULL,
    model VARCHAR(120) NOT NULL,
    plate VARCHAR(50) NOT NULL
);

CREATE TABLE IF NOT EXISTS service_item (
    id SERIAL PRIMARY KEY,
    title VARCHAR(200) NOT NULL,
    price NUMERIC(10,2) NOT NULL
);

CREATE TABLE IF NOT EXISTS work_order (
    id SERIAL PRIMARY KEY,
    vehicle_id INTEGER NOT NULL REFERENCES vehicle(id) ON DELETE CASCADE,
    note TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS work_order_item (
    id SERIAL PRIMARY KEY,
    work_order_id INTEGER NOT NULL REFERENCES work_order(id) ON DELETE CASCADE,
    service_item_id INTEGER NOT NULL REFERENCES service_item(id),
    quantity INTEGER NOT NULL
);

INSERT INTO service_item(title, price) VALUES
    ('Ölwechsel', 89.00),
    ('Bremsflüssigkeit wechseln', 59.00),
    ('Reifen wechseln (pro Stück)', 18.00),
    ('Klimaanlagen-Service', 99.00),
    ('Bremsbeläge erneuern vorne', 149.00),
    ('Zündkerzen austauschen', 79.00),
    ('Diagnose / Fehlerspeicher auslesen', 25.00),
    ('Arbeitszeit pro Stunde', 75.00)
ON CONFLICT DO NOTHING;
