-- Snapshot cijene u trenutku rezervacije
ALTER TABLE `Appointment` ADD COLUMN `price` DOUBLE NOT NULL DEFAULT 0;

-- Procjena za postojeće termine (trenutne cijene usluge + opcija)
UPDATE `Appointment` AS a
INNER JOIN `Service` AS s ON s.id = a.serviceId
SET a.price = COALESCE(s.price, 0) + COALESCE(
  (
    SELECT SUM(v.priceDelta)
    FROM `AppointmentOptionValue` AS aov
    INNER JOIN `ServiceOptionValue` AS v ON v.id = aov.optionValueId
    WHERE aov.appointmentId = a.id
  ),
  0
);
