<?php

declare(strict_types=1);

require_once __DIR__ . '/../Core/Database.php';

session_start();

$pdo = Database::connection();

if (empty($_SESSION['admin_id'])) {
    header('Location: ../public/login.php');
    exit;
}

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

if ($id <= 0) {
    die('Invalid application ID.');
}

$stmt = $pdo->prepare("

    SELECT

        a.*,

        u.name AS university_name,

        u.code AS university_code,

        u.application_url,

        d.name AS department_name,

        p.option_name

    FROM applications a

    LEFT JOIN universities u
        ON u.id = a.university_id

    LEFT JOIN departments d
        ON d.id = a.department_id

    LEFT JOIN programs p
        ON p.id = a.program_id

    WHERE a.id = ?

    LIMIT 1
");

$stmt->execute([$id]);

$application =
    $stmt->fetch(
        PDO::FETCH_ASSOC
    );

if (!$application) {
    die('Application not found.');
}

$portal =
    trim(
        $application['application_url']
        ?? ''
    );

if ($portal === '') {

    die(
        'Application portal is not configured '
        . 'for this university.'
    );
}

/*
|--------------------------------------------------------------------------
| MARK APPLICATION AS PROCESSING
|--------------------------------------------------------------------------
*/

if ($application['status'] !== 'PROCESSING') {

    $stmt = $pdo->prepare("
        UPDATE applications
        SET status = 'PROCESSING'
        WHERE id = ?
    ");

    $stmt->execute([$id]);

    try {

        $stmt = $pdo->prepare("
            INSERT INTO
                application_status_history
            (
                application_id,
                old_status,
                new_status
            )
            VALUES
            (?, ?, ?)
        ");

        $stmt->execute([
            $id,
            $application['status'],
            'PROCESSING'
        ]);

    } catch (Throwable $e) {
        // Status update should still work.
    }
}

/*
|--------------------------------------------------------------------------
| SAVE START INFORMATION IN SESSION
|--------------------------------------------------------------------------
|
| This gives the next mapping/automation layer access
| to the selected student.
|
*/

$_SESSION['active_application'] = [
    'id'
        => (int)$application['id'],

    'application_number'
        => $application[
            'application_number'
        ],

    'university_id'
        => (int)$application[
            'university_id'
        ],

    'university_name'
        => $application[
            'university_name'
        ],

    'university_code'
        => $application[
            'university_code'
        ],

    'application_url'
        => $portal,

    'full_name'
        => $application[
            'full_name'
        ],

    'email'
        => $application[
            'email'
        ],

    'phone'
        => $application[
            'phone'
        ],

    'index_number'
        => $application[
            'index_number'
        ],

    'nid_passport'
        => $application[
            'nid_passport'
        ],

    'father_name'
        => $application[
            'father_name'
        ],

    'father_phone'
        => $application[
            'father_phone'
        ],

    'mother_name'
        => $application[
            'mother_name'
        ],

    'mother_phone'
        => $application[
            'mother_phone'
        ],

    'province'
        => $application[
            'province'
        ],

    'district'
        => $application[
            'district'
        ],

    'sector'
        => $application[
            'sector'
        ],

    'cell'
        => $application[
            'cell'
        ],

    'village'
        => $application[
            'village'
        ],

    'address'
        => $application[
            'address'
        ],

    'education_school'
        => $application[
            'education_school'
        ],

    'education_combination'
        => $application[
            'education_combination'
        ],

    'education_year'
        => $application[
            'education_year'
        ],

    'applicant_type'
        => $application[
            'applicant_type'
        ],

    'passport_photo'
        => $application[
            'passport_photo'
        ],

    'nid_photo'
        => $application[
            'nid_photo'
        ],

    'result_slip'
        => $application[
            'result_slip'
        ],

    'department'
        => $application[
            'department_name'
        ],

    'programme'
        => $application[
            'option_name'
        ]
];

/*
|--------------------------------------------------------------------------
| OPEN UNIVERSITY PORTAL
|--------------------------------------------------------------------------
*/

header(
    'Location: ' . $portal
);

exit;