<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    redirect('app/modules/licenses/index.php');
}

require_valid_csrf();

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

if (!player_license_tables_ready()) {
    flash('error', ['type' => 'danger', 'message' => 'Player licence services are temporarily unavailable.']);
    redirect('app/modules/licenses/index.php');
}

if ($action === 'issue_license') {
    license_module_boot();

    $playerId = (int) ($_POST['player_id'] ?? 0);
    $playerRecord = find_player_or_abort($playerId);
    $licenseId = issue_player_license($playerId, (int) current_user()['id']);

    audit_log('issue_player_license', 'player_licenses', $licenseId, null, [
        'player_id' => $playerId,
        'registration_number' => $playerRecord['registration_number'],
    ]);

    flash('success', ['type' => 'success', 'message' => 'Player licence issued successfully.']);
    redirect('app/modules/licenses/view.php?player_id=' . $playerId);
}

if ($action === 'set_license_status') {
    license_module_boot();

    $playerId = (int) ($_POST['player_id'] ?? 0);
    $status = (string) ($_POST['status'] ?? '');

    if (!in_array($status, ['active', 'suspended', 'revoked'], true)) {
        flash('error', ['type' => 'danger', 'message' => 'Invalid licence status.']);
        redirect('app/modules/licenses/view.php?player_id=' . $playerId);
    }

    $existingLicense = get_player_license_by_player_id($playerId);
    set_player_license_status($playerId, $status, (int) current_user()['id']);

    audit_log('set_player_license_status', 'player_licenses', (int) ($existingLicense['id'] ?? 0), $existingLicense, ['status' => $status, 'player_id' => $playerId]);

    flash('success', ['type' => 'success', 'message' => 'Player licence status updated.']);
    redirect('app/modules/licenses/view.php?player_id=' . $playerId);
}

if ($action === 'verify_license') {
    license_verification_module_boot();

    $fixtureId = (int) ($_POST['fixture_id'] ?? 0);
    $playerId = (int) ($_POST['player_id'] ?? 0);
    $fixture = find_fixture_or_abort($fixtureId);

    if (!player_license_player_belongs_to_fixture($playerId, $fixture)) {
        abort_request(403, 'Fixture mismatch', 'The selected player does not belong to this fixture.');
    }

    if (!fixture_license_verification_is_open($fixture)) {
        flash('error', ['type' => 'danger', 'message' => 'Licence verification is only open before kickoff.']);
        redirect('app/modules/licenses/verify.php?fixture_id=' . $fixtureId);
    }

    $license = get_player_license_by_player_id($playerId);
    if (!player_license_is_verifiable($license)) {
        flash('error', ['type' => 'danger', 'message' => 'Only active, unexpired licences can be verified.']);
        redirect('app/modules/licenses/verify.php?fixture_id=' . $fixtureId);
    }

    verify_player_license_for_fixture($fixtureId, $playerId, (int) $license['id'], (int) current_user()['id']);
    audit_log('verify_player_license', 'player_license_checks', $fixtureId, null, ['fixture_id' => $fixtureId, 'player_id' => $playerId, 'license_id' => $license['id']]);

    flash('success', ['type' => 'success', 'message' => 'Player licence verified for this fixture.']);
    redirect('app/modules/licenses/verify.php?fixture_id=' . $fixtureId);
}

if ($action === 'clear_verification') {
    license_verification_module_boot();

    $fixtureId = (int) ($_POST['fixture_id'] ?? 0);
    $playerId = (int) ($_POST['player_id'] ?? 0);
    $fixture = find_fixture_or_abort($fixtureId);

    if (!player_license_player_belongs_to_fixture($playerId, $fixture)) {
        abort_request(403, 'Fixture mismatch', 'The selected player does not belong to this fixture.');
    }

    clear_player_license_verification($fixtureId, $playerId);
    audit_log('clear_player_license_verification', 'player_license_checks', $fixtureId, null, ['fixture_id' => $fixtureId, 'player_id' => $playerId]);

    flash('success', ['type' => 'success', 'message' => 'Fixture licence verification cleared.']);
    redirect('app/modules/licenses/verify.php?fixture_id=' . $fixtureId);
}

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