package de.antco.projekt.dao;

import de.antco.projekt.Database;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class SchemaCreator {
    public void ensureSchema() throws SQLException {
        try (Connection con = Database.getConnection(); Statement st = con.createStatement()) {
            st.execute("CREATE TABLE IF NOT EXISTS person (" +
                    "id SERIAL PRIMARY KEY, " +
                    "name VARCHAR(200) NOT NULL, " +
                    "phone VARCHAR(100)" +
                    ")");
            st.execute("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" +
                    ")");
            st.execute("CREATE TABLE IF NOT EXISTS service_item (" +
                    "id SERIAL PRIMARY KEY, " +
                    "title VARCHAR(200) NOT NULL, " +
                    "price NUMERIC(10,2) NOT NULL" +
                    ")");
            st.execute("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()" +
                    ")");
            st.execute("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" +
                    ")");
        }
    }
}
