<?php
session_start();
require_once '../../config/database.php';
require_once '../../config/function.php';

// Cek login
checkLogin();

// Cek method dan ID
if ($_SERVER['REQUEST_METHOD'] != 'GET' || !isset($_GET['id'])) {
    $_SESSION['modal_title'] = 'Error!';
    $_SESSION['modal_message'] = 'Akses tidak valid';
    $_SESSION['modal_type'] = 'danger';
    header('Location: index.php');
    exit;
}

$id = (int)$_GET['id'];

// Ambil data guru untuk informasi
$query = "SELECT g.*, u.username, u.role 
          FROM bkcare_guru g
          LEFT JOIN bkcare_users u ON g.id_user = u.id_user
          WHERE g.id_guru = $id";
$result = mysqli_query($conn, $query);
$data = mysqli_fetch_assoc($result);

if (!$data) {
    $_SESSION['modal_title'] = 'Error!';
    $_SESSION['modal_message'] = 'Data guru tidak ditemukan';
    $_SESSION['modal_type'] = 'danger';
    header('Location: index.php');
    exit;
}

// CEK: Apakah guru ini digunakan di tabel lain?
$check_kelas = mysqli_query($conn, "SELECT COUNT(*) as total FROM bkcare_kelas WHERE wali_kelas = $id");
$check_jadwal = mysqli_query($conn, "SELECT COUNT(*) as total FROM bkcare_jadwal_konseling WHERE id_guru_bk = $id");
$check_konseling = mysqli_query($conn, "SELECT COUNT(*) as total FROM bkcare_konseling WHERE id_guru_bk = $id");

$total_used = 0;
$used_in = [];

if ($row = mysqli_fetch_assoc($check_kelas)) {
    if ($row['total'] > 0) {
        $total_used += $row['total'];
        $used_in[] = $row['total'] . ' data kelas (sebagai wali kelas)';
    }
}
if ($row = mysqli_fetch_assoc($check_jadwal)) {
    if ($row['total'] > 0) {
        $total_used += $row['total'];
        $used_in[] = $row['total'] . ' jadwal konseling';
    }
}
if ($row = mysqli_fetch_assoc($check_konseling)) {
    if ($row['total'] > 0) {
        $total_used += $row['total'];
        $used_in[] = $row['total'] . ' sesi konseling';
    }
}

if ($total_used > 0) {
    $_SESSION['modal_title'] = 'Tidak Dapat Dihapus!';
    $_SESSION['modal_message'] = 'Guru <strong>' . htmlspecialchars($data['nama_guru']) . '</strong> sedang digunakan oleh: <br><strong>' . implode(', ', $used_in) . '</strong><br><br>Hapus atau pindahkan data terkait terlebih dahulu.';
    $_SESSION['modal_type'] = 'warning';
    header('Location: index.php');
    exit;
}

// Mulai transaksi
mysqli_begin_transaction($conn);

try {
    // Hapus data guru
    $delete = mysqli_query($conn, "DELETE FROM bkcare_guru WHERE id_guru = $id");
    
    if (!$delete) {
        throw new Exception('Gagal menghapus data guru: ' . mysqli_error($conn));
    }
    
    mysqli_commit($conn);
    
    $_SESSION['modal_title'] = 'Berhasil!';
    $_SESSION['modal_message'] = 'Data guru <strong>' . htmlspecialchars($data['nama_guru']) . '</strong> berhasil dihapus';
    $_SESSION['modal_type'] = 'success';
    
} catch (Exception $e) {
    mysqli_rollback($conn);
    
    $_SESSION['modal_title'] = 'Gagal!';
    $_SESSION['modal_message'] = $e->getMessage();
    $_SESSION['modal_type'] = 'danger';
}

header('Location: index.php');
exit;
?>