<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Content-Type: application/json");
if ($_SERVER["REQUEST_METHOD"] === "OPTIONS") exit;
require "../../config/database.php";
require "../../helpers/response.php";

$id = isset($_POST["id"]) ? (int) $_POST["id"] : 0;
$validasiMenu = trim($_POST["validasi_menu"] ?? "belum");
$validasiJumlah = trim($_POST["validasi_jumlah"] ?? "belum");
$validasiKondisi = trim($_POST["validasi_kondisi"] ?? "belum");
$jumlahDiterima = isset($_POST["jumlah_diterima"]) ? (int) $_POST["jumlah_diterima"] : 0;
$catatanValidasi = trim($_POST["catatan_validasi"] ?? "");

if ($id <= 0) response(false, "ID pengiriman tidak valid", null);
if (!in_array($validasiMenu, ["sesuai", "tidak_sesuai"])) response(false, "Validasi menu wajib sesuai atau tidak_sesuai", null);
if (!in_array($validasiJumlah, ["sesuai", "tidak_sesuai"])) response(false, "Validasi jumlah wajib sesuai atau tidak_sesuai", null);
if (!in_array($validasiKondisi, ["baik", "kurang_baik", "bermasalah"])) response(false, "Validasi kondisi tidak valid", null);
if ($jumlahDiterima <= 0) response(false, "Jumlah diterima wajib lebih dari 0", null);

$uploadDir = "../../uploads/pengiriman/";
if (!is_dir($uploadDir)) mkdir($uploadDir, 0777, true);

if (!isset($_FILES["foto_penerimaan"]) || $_FILES["foto_penerimaan"]["error"] !== UPLOAD_ERR_OK) {
    response(false, "Foto penerimaan dari Pihak Sekolah wajib diupload", null);
}

$allowed = ["jpg", "jpeg", "png", "webp"];
$ext = strtolower(pathinfo($_FILES["foto_penerimaan"]["name"], PATHINFO_EXTENSION));
if (!in_array($ext, $allowed)) response(false, "Format foto penerimaan harus jpg, jpeg, png, atau webp", null);

$newName = "foto_penerimaan_" . time() . "_" . mt_rand(1000, 9999) . "." . $ext;
$target = $uploadDir . $newName;
if (!move_uploaded_file($_FILES["foto_penerimaan"]["tmp_name"], $target)) response(false, "Gagal upload foto penerimaan", null);

$fotoPenerimaan = "uploads/pengiriman/" . $newName;

$status = "selesai";
if ($validasiMenu !== "sesuai" || $validasiJumlah !== "sesuai" || $validasiKondisi === "bermasalah") {
    $status = "bermasalah";
}

$sql = "UPDATE tracking_pengiriman
SET status = ?, foto_penerimaan = ?, jumlah_diterima = ?, validasi_menu = ?, validasi_jumlah = ?, validasi_kondisi = ?, catatan_validasi = ?, waktu_validasi = NOW(), waktu_diterima = NOW(), waktu_selesai = CASE WHEN ? = 'selesai' THEN NOW() ELSE waktu_selesai END, updated_at = NOW()
WHERE id = ?";

$stmt = $conn->prepare($sql);
if (!$stmt) response(false, "Query gagal disiapkan: " . $conn->error, null);

$stmt->bind_param("ssisssssi", $status, $fotoPenerimaan, $jumlahDiterima, $validasiMenu, $validasiJumlah, $validasiKondisi, $catatanValidasi, $status, $id);

if (!$stmt->execute()) response(false, "Gagal menyimpan validasi: " . $stmt->error, null);

response(true, "Validasi Pihak Sekolah berhasil disimpan", [
  "id" => $id,
  "status" => $status,
  "foto_penerimaan" => $fotoPenerimaan
]);
?>
