<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

include "../../imagekit/vendor/autoload.php";
include "../include/config.php";

use ImageKit\ImageKit;

/* ---------------------------
   Check login
---------------------------- */
if (!isset($_SESSION['studentid'])) {
    header("Location: ../index.php");
    exit();
}

if (!isset($_POST['updateprofile'])) {
    header("Location: account.php");
    exit();
}

$id        = mysqli_real_escape_string($conn, $_POST['id']);
$studentid = $_SESSION['studentid'];

/* ---------------------------
   ImageKit Init
---------------------------- */
$imageKit = new ImageKit(
    "public_EypymL+l9sKGk/JHnWApsJ94ybA=",
    "private_T5LPQkxAMv70EVlUDk+RyUU3h5w=",
    "https://ik.imagekit.io/zamastudents"
);

/* ---------------------------
   Get existing image
---------------------------- */
$image_url = '';
$q = mysqli_query($conn, "SELECT image FROM students WHERE id='$id'");
if ($q && mysqli_num_rows($q) > 0) {
    $image_url = mysqli_fetch_assoc($q)['image'];
}

/* ---------------------------
   Upload to ImageKit (ROOT "/")
---------------------------- */
if (!empty($_FILES['image']['name'])) {

    if ($_FILES['image']['error'] !== 0) {
        $_SESSION['error'] = "Upload failed";
        header("Location: account.php");
        exit();
    }

    $upload = $imageKit->upload([
        'file'     => fopen($_FILES['image']['tmp_name'], 'r'),
        'fileName' => 'student_'.$studentid.'_'.time(),
        'folder'   => '/',          // ROOT folder
        'useUniqueFileName' => true
    ]);

    if (isset($upload->error)) {
        $_SESSION['error'] = "ImageKit error: ".$upload->error->message;
        header("Location: account.php");
        exit();
    }

    $image_url = $upload->result->url; // ✅ SAVE THIS
}

/* ---------------------------
   Update profile
---------------------------- */
$studentname     = mysqli_real_escape_string($conn, $_POST['studentname']);
$mobile          = mysqli_real_escape_string($conn, $_POST['mobile']);
$email           = mysqli_real_escape_string($conn, $_POST['email']);
$gender          = mysqli_real_escape_string($conn, $_POST['gender']);
$parent_name     = mysqli_real_escape_string($conn, $_POST['parent_name']);
$parent_mobile   = mysqli_real_escape_string($conn, $_POST['parent_mobile']);
$parent_email    = mysqli_real_escape_string($conn, $_POST['parent_email']);
$parent_relation = mysqli_real_escape_string($conn, $_POST['parent_relation']);

$sql = "UPDATE students SET
            studentname     = '$studentname',
            mobile          = '$mobile',
            email           = '$email',
            gender          = '$gender',
            parent_name     = '$parent_name',
            parent_mobile   = '$parent_mobile',
            parent_email    = '$parent_email',
            parent_relation = '$parent_relation',
            image           = '$image_url'
        WHERE id='$id' AND studentid='$studentid'";

if (mysqli_query($conn, $sql)) {
    $_SESSION['success'] = "Profile updated successfully";
} else {
    $_SESSION['error'] = mysqli_error($conn);
}

header("Location: account.php");
exit();
