package de.antco.projekt.service;

import de.antco.projekt.dao.PersonDao;
import de.antco.projekt.dao.SchemaCreator;
import de.antco.projekt.dao.ServiceItemDao;
import de.antco.projekt.dao.VehicleDao;
import de.antco.projekt.dao.WorkOrderDao;
import de.antco.projekt.model.Person;
import de.antco.projekt.model.ServiceItem;
import de.antco.projekt.model.Vehicle;
import de.antco.projekt.model.WorkOrderDetails;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class WorkshopService {
    private final PersonDao personDao = new PersonDao();
    private final VehicleDao vehicleDao = new VehicleDao();
    private final ServiceItemDao serviceItemDao = new ServiceItemDao();
    private final WorkOrderDao workOrderDao = new WorkOrderDao();

    public WorkshopService() {
        try {
            new SchemaCreator().ensureSchema();
            serviceItemDao.seedDefaultItems();
        } catch (SQLException e) {
            throw new RuntimeException("Setup fehlgeschlagen: " + e.getMessage(), e);
        }
    }

    public Person createPerson(String name, String phone) throws SQLException {
        return personDao.create(name, phone);
    }

    public List<Person> listPersons() throws SQLException {
        return personDao.findAll();
    }

    public Vehicle createVehicle(int personId, String brand, String model, String plate) throws SQLException {
        return vehicleDao.create(personId, brand, model, plate);
    }

    public List<Vehicle> listVehicles(int personId) throws SQLException {
        return vehicleDao.findByPerson(personId);
    }

    public List<ServiceItem> listServiceItems() throws SQLException {
        return serviceItemDao.findAll();
    }

    public int createWorkOrder(int vehicleId, String note, Map<Integer, Integer> items) throws SQLException {
        return workOrderDao.createWorkOrder(vehicleId, note, items);
    }

    public List<WorkOrderDetails> listWorkOrders() throws SQLException {
        return workOrderDao.fetchAllDetails();
    }

    public Person findPerson(int id) throws SQLException {
        return personDao.findById(id);
    }

    public Vehicle findVehicle(int id) throws SQLException {
        return vehicleDao.findById(id);
    }
}
