<?php
    header
('Content-Type: application/json');
    require_once 
'../DB/db_credentials.php';
    require_once 
'../DB/db_connection.php';

    
$id             $_POST['id']          ?? '';
    
$date           $_POST['date']        ?? '';
    
$fromTime       $_POST['fromTime']    ?? '';
    
$toTime         $_POST['toTime']      ?? '';
    
$extraTimeRaw   trim($_POST['extraTime'] ?? '');
    
$extraTime      = ($extraTimeRaw === '') ? null $extraTimeRaw;
    
$fk_extra       $_POST['fk_extra']    ?? '';
    
$fk_film        $_POST['fk_film']     ?? '';
    
$fk_responsible $_POST['fk_responsible'] ?? '';

    if (!
$id || !$date || !$fromTime || !$toTime || !$fk_extra || !$fk_film) {
        
http_response_code(400);
        echo 
json_encode(['status' => 'error''message' => 'Missing required fields.']);
        exit;
    }

    
// check if shift has registrations
    
$sql "SELECT COUNT(*) FROM citeLeParis_register WHERE fk_shift = ?";
    
$stmt mysqli_prepare($dbc$sql);
    
mysqli_stmt_bind_param($stmt'i'$id);
    
mysqli_stmt_execute($stmt);
    
mysqli_stmt_bind_result($stmt$registrationCount);
    
mysqli_stmt_fetch($stmt);
    
mysqli_stmt_close($stmt);

    
// check if shift is outdated
    
$sql "SELECT date, fromTime, toTime, extraTime, fk_extra, fk_film, fk_responsible FROM citeLeParis_shift WHERE pk_shift = ?";
    
$stmt mysqli_prepare($dbc$sql);
    
mysqli_stmt_bind_param($stmt'i'$id);
    
mysqli_stmt_execute($stmt);
    
mysqli_stmt_bind_result($stmt$oldDate$oldFrom$oldTo$oldExtraTime$oldExtra$oldFilm$oldRespo);

    if (!
mysqli_stmt_fetch($stmt)) {
        
http_response_code(404);
        echo 
json_encode(['status' => 'error''message' => 'Shift not found.']);
        
mysqli_stmt_close($stmt);
        
mysqli_close($dbc);
        exit;
    }
    
mysqli_stmt_close($stmt);

    
// outdated if date is before today
    
$isOutdated strtotime($oldDate) < strtotime(date('Y-m-d'));

    
$extraTimeNormalized $extraTime === '' || $extraTime === null null $extraTime;
    
$oldExtraTimeNormalized $oldExtraTime === '' || $oldExtraTime === null null $oldExtraTime;

    
$onlyResponsibleChanged =
        
$date == $oldDate &&
        
$fromTime == $oldFrom &&
        
$toTime == $oldTo &&
        
$extraTimeNormalized == $oldExtraTimeNormalized &&
        
$fk_extra == $oldExtra &&
        
$fk_film == $oldFilm &&
        
$fk_responsible != $oldRespo;

    if (
$registrationCount && !$onlyResponsibleChanged) {
        
http_response_code(403);
        echo 
json_encode(['status' => 'error''message' => 'This shift has registrations. Only the responsible person can be changed.']);
        
mysqli_close($dbc);
        exit;
    }

    
// if only responsible changed, but shift is outdated
    
if ($onlyResponsibleChanged && $isOutdated) {
        
http_response_code(403);
        echo 
json_encode(['status' => 'error''message' => 'Cannot change responsible person for outdated shift.']);
        
mysqli_close($dbc);
        exit;
    }

    
$sql "UPDATE citeLeParis_shift
            SET date = ?, fromTime = ?, toTime = ?, extraTime = " 
. ($extraTime === null "NULL" "?") . ",
                fk_extra = ?, fk_film = ?, fk_responsible = ?
            WHERE pk_shift = ?"
;

    
$stmt mysqli_prepare($dbc$sql);
    if (!
$stmt) {
        
http_response_code(500);
        echo 
json_encode(['status' => 'error''message' => 'Failed to prepare update statement.']);
        
mysqli_close($dbc);
        exit;
    }

    if (
$extraTime === null) {
        
mysqli_stmt_bind_param($stmt'sssiiii'$date$fromTime$toTime$fk_extra$fk_film$fk_responsible$id);
    } else {
        
mysqli_stmt_bind_param($stmt'ssssiiii'$date$fromTime$toTime$extraTime$fk_extra$fk_film$fk_responsible$id);
    }

    
$ok mysqli_stmt_execute($stmt);
    
mysqli_stmt_close($stmt);
    
mysqli_close($dbc);

    if (!
$ok) {
        
http_response_code(500);
        echo 
json_encode(['status' => 'error''message' => 'Update failed.']);
        exit;
    }

    
http_response_code(200);
    echo 
json_encode(['status' => 'success''message' => 'Shift updated successfully.']);
?>