<?php
// ═══════════════════════════════════════════════════════════════
// api/profil_siswa.php
// Endpoint: GET /spp_admin/api/profil_siswa.php?siswa_id=123
// Dipanggil oleh ApiRepository.getProfilSiswa()
// ═══════════════════════════════════════════════════════════════
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Authorization, Content-Type');

require_once '../config/database.php';

$siswaId = (int)($_GET['siswa_id'] ?? 0);
if ($siswaId <= 0) {
    echo json_encode(['status' => 'error', 'message' => 'siswa_id wajib diisi']);
    exit;
}

try {
    $st = $pdo->prepare(
        "SELECT id, nama, nis, kelas, jurusan, foto, no_hp, alamat, status
         FROM siswa WHERE id = ? AND status = 'aktif' LIMIT 1"
    );
    $st->execute([$siswaId]);
    $siswa = $st->fetch(PDO::FETCH_ASSOC);

    if (!$siswa) {
        echo json_encode(['status' => 'error', 'message' => 'Siswa tidak ditemukan']);
        exit;
    }

    echo json_encode([
        'status' => 'success',
        'data'   => [
            'id'      => (int)$siswa['id'],
            'nama'    => $siswa['nama']    ?? '',
            'nis'     => $siswa['nis']     ?? '',
            'kelas'   => $siswa['kelas']   ?? '',
            'jurusan' => $siswa['jurusan'] ?? '',
            'foto'    => $siswa['foto']    ?? '',  // nama file saja, misal: "foto_123_abc.jpg"
            'no_hp'   => $siswa['no_hp']   ?? '',
            'alamat'  => $siswa['alamat']  ?? '',
        ]
    ]);
} catch (Exception $e) {
    echo json_encode(['status' => 'error', 'message' => 'Database error']);
}