<?php

use App\Models\StudentCardSettingModel;
use App\Models\SchoolSettingModel;

// ─────────────────────────────────────────────────────────────
// SETTINGS
// ─────────────────────────────────────────────────────────────

function getCardSettings()
{
    $model  = new StudentCardSettingModel();
    $card   = $model->getSettings() ?? [];

    $school = (new SchoolSettingModel())->getSettings() ?? [];

    return array_merge($card, [
        'school_name'    => $school['school_name']    ?? '',
        'school_address' => $school['address']        ?? '',
        'school_phone'   => $school['phone']          ?? '',
        'school_email'   => $school['email']          ?? '',
        'district'       => $school['city']           ?? '',
        'headmaster_name'=> $school['principal_name'] ?? '',
        'headmaster_nip' => $school['principal_nip']  ?? '',
        'logo_image'     => $school['logo_kemenag'] ?? $school['logo'] ?? '',
    ]);
}

// ─────────────────────────────────────────────────────────────
// FOTO SISWA
// ─────────────────────────────────────────────────────────────

function getStudentPhotoUrl($photo)
{
    if ($photo) {
        return \App\Services\StorageService::getUrl($photo, 'students');
    }
    return 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0iI2EwYWVjMCI+PHJlY3Qgd2lkdGg9IjI0IiBoZWlnaHQ9IjI0IiBmaWxsPSIjZWRmMmY3Ii8+PHBhdGggZD0iTTEyIDEyYzIuMjEgMCA0LTEuNzkgNC00cy0xLjc5LTQtNC00LTQgMS43OS00IDQgMS43OSA0IDQgNHptMCAyYy0yLjY3IDAtOCAxLjM0LTggNHYyaDE2di0yYzAtMi42Ni01LjMzLTQtOC00eiIvPjwvc3ZnPg==';
}

// ─────────────────────────────────────────────────────────────
// FORMAT DATA
// ─────────────────────────────────────────────────────────────

function formatStudentBirthInfo($student)
{
    $place = trim($student['birth_place'] ?? '');
    $date  = '';
    if (!empty($student['birth_date']) && $student['birth_date'] !== '0000-00-00') {
        $date = date('d/m/Y', strtotime($student['birth_date']));
    }
    if ($place && $date) return $place . ', ' . $date;
    return $place ?: $date ?: '-';
}

function formatStudentAddress($student)
{
    $parts = [];
    
    if (!empty($student['address'])) $parts[] = $student['address'];
    if (!empty($student['village'])) $parts[] = $student['village'];
    if (!empty($student['sub_district'])) $parts[] = $student['sub_district'];
    
    // District dan City: jika sama, tampilkan satu saja
    $district = $student['district'] ?? '';
    $city = $student['city'] ?? '';
    
    if (!empty($district) && !empty($city) && $district === $city) {
        $parts[] = $district;
    } else {
        if (!empty($district)) $parts[] = $district;
        if (!empty($city)) $parts[] = $city;
    }
    
    if (!empty($student['province'])) $parts[] = $student['province'];
    if (!empty($student['postal_code'])) $parts[] = $student['postal_code'];
    
    return implode(', ', $parts);
}

function formatGender($gender)
{
    return ($gender === 'L') ? 'Laki-laki' : 'Perempuan';
}

function formatCardDate($dateStr)
{
    if (empty($dateStr)) return date('j F Y');
    $bulan = ['','Januari','Februari','Maret','April','Mei','Juni',
              'Juli','Agustus','September','Oktober','November','Desember'];
    $ts = strtotime($dateStr);
    return date('j', $ts) . ' ' . $bulan[(int)date('n', $ts)] . ' ' . date('Y', $ts);
}

// ─────────────────────────────────────────────────────────────
// QR CODE — OFFLINE PURE PHP (no composer, no internet)
// Menggunakan QR Code versi 3 (Level M) dengan Reed-Solomon
// Output: data URI base64 PNG
// ─────────────────────────────────────────────────────────────

function generateQRCode($data, $pixelSize = 4)
{
    if (empty($data)) return '';

    try {
        $options = new \chillerlan\QRCode\QROptions;
        $options->outputType    = \chillerlan\QRCode\Output\QRGdImagePNG::class;
        $options->eccLevel      = \chillerlan\QRCode\Common\EccLevel::M;
        $options->scale         = 6;
        $options->imageBase64   = true;
        $options->quietzoneSize = 1;

        return (new \chillerlan\QRCode\QRCode($options))->render($data);
    } catch (\Exception $e) {
        return '';
    }
}