<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

$refereeModuleBoot = 'referee_module_boot';
$validateRefereePayload = 'validate_referee_payload';
$findRefereeOrAbort = 'find_referee_or_abort';
$refereeHasDependencies = 'referee_has_dependencies';

$refereeModuleBoot();

if (!is_post()) {
    redirect('app/modules/referees/index.php');
}

require_valid_csrf();

$action = (string) ($_POST['action'] ?? '');

if ($action === 'create') {
    remember_old_input($_POST);
    $errors = [];
    $payload = $validateRefereePayload($_POST, $errors);

    if ($errors !== []) {
        flash('errors', $errors);
        flash('error', ['type' => 'danger', 'message' => 'Please correct the referee form errors.']);
        redirect('app/modules/referees/create.php');
    }

    db_query(
        'INSERT INTO referees (full_name, phone, email, level, status, created_at)
         VALUES (:full_name, :phone, :email, :level, :status, NOW())',
        $payload
    );

    $refereeId = (int) db()->lastInsertId();
    audit_log('create_referee', 'referees', $refereeId, null, $payload);
    clear_old_input();
    flash('success', ['type' => 'success', 'message' => 'Referee created successfully.']);
    redirect('app/modules/referees/view.php?id=' . $refereeId);
}

if ($action === 'update') {
    $refereeId = (int) ($_POST['id'] ?? 0);
    $existingReferee = $findRefereeOrAbort($refereeId);
    remember_old_input($_POST);
    $errors = [];
    $payload = $validateRefereePayload($_POST, $errors, $refereeId);

    if ($errors !== []) {
        flash('errors', $errors);
        flash('error', ['type' => 'danger', 'message' => 'Please correct the referee form errors.']);
        redirect('app/modules/referees/edit.php?id=' . $refereeId);
    }

    db_query(
        'UPDATE referees
         SET full_name = :full_name,
             phone = :phone,
             email = :email,
             level = :level,
             status = :status
         WHERE id = :id',
        $payload + ['id' => $refereeId]
    );

    audit_log('update_referee', 'referees', $refereeId, $existingReferee, $payload);
    clear_old_input();
    flash('success', ['type' => 'success', 'message' => 'Referee updated successfully.']);
    redirect('app/modules/referees/view.php?id=' . $refereeId);
}

if ($action === 'delete') {
    $refereeId = (int) ($_POST['id'] ?? 0);
    $existingReferee = $findRefereeOrAbort($refereeId);

    if ($refereeHasDependencies($refereeId)) {
        flash('error', ['type' => 'danger', 'message' => 'This referee cannot be deleted while fixtures are assigned.']);
        redirect('app/modules/referees/delete.php?id=' . $refereeId);
    }

    db_query('DELETE FROM referees WHERE id = :id', ['id' => $refereeId]);
    audit_log('delete_referee', 'referees', $refereeId, $existingReferee, null);
    flash('success', ['type' => 'success', 'message' => 'Referee deleted successfully.']);
    redirect('app/modules/referees/index.php');
}

redirect('app/modules/referees/index.php');
