<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

$transferModuleBoot = 'transfer_module_boot';
$validateTransferWindowPayload = 'validate_transfer_window_payload';
$transferCreateWindow = 'transfer_create_window';
$transferSetWindowStatus = 'transfer_set_window_status';
$validateTransferInterestPayload = 'validate_transfer_interest_payload';
$transferRegisterInterest = 'transfer_register_interest';
$validateTransferReviewPayload = 'validate_transfer_review_payload';
$transferRecordSellingClubDecision = 'transfer_record_selling_club_decision';
$validateTransferOfferPayload = 'validate_transfer_offer_payload';
$transferSubmitOffer = 'transfer_submit_offer';
$validateTransferOfferReviewPayload = 'validate_transfer_offer_review_payload';
$transferRecordOfferDecision = 'transfer_record_offer_decision';
$validateTransferApprovalPayload = 'validate_transfer_approval_payload';
$transferRecordApprovalDecision = 'transfer_record_approval_decision';
$validateTransferMedicalPayload = 'validate_transfer_medical_payload';
$transferRecordMedicalDecision = 'transfer_record_medical_decision';

$transferModuleBoot();

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

require_valid_csrf();

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

if ($action === 'register_interest') {
    remember_old_input($_POST);

    $errors = [];
    $payload = $validateTransferInterestPayload($_POST, $errors);

    if ($errors !== []) {
        flash('errors', $errors);
        flash('error', ['type' => 'danger', 'message' => 'Please correct the transfer interest form errors.']);
        redirect('app/modules/transfers/create-interest.php?player_id=' . (int) ($payload['player_id'] ?? 0));
    }

    try {
        $transferId = $transferRegisterInterest($payload);
    } catch (Throwable $exception) {
        flash('error', ['type' => 'danger', 'message' => 'Transfer interest could not be registered.']);
        redirect('app/modules/transfers/create-interest.php?player_id=' . (int) $payload['player_id']);
    }

    clear_old_input();
    flash('success', ['type' => 'success', 'message' => 'Transfer interest registered successfully and published to the transfer news desk.']);
    redirect('app/modules/transfers/index.php?transfer_id=' . $transferId);
}

if ($action === 'create_window') {
    remember_old_input($_POST);

    $errors = [];
    $payload = $validateTransferWindowPayload($_POST, $errors);

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

    try {
        $transferCreateWindow($payload);
    } catch (Throwable $exception) {
        flash('error', ['type' => 'danger', 'message' => 'The transfer window could not be created.']);
        redirect('app/modules/transfers/index.php');
    }

    clear_old_input();
    flash('success', ['type' => 'success', 'message' => 'Transfer window created successfully.']);
    redirect('app/modules/transfers/index.php');
}

if ($action === 'set_window_status') {
    $windowId = (int) ($_POST['window_id'] ?? 0);
    $status = trim((string) ($_POST['status'] ?? ''));

    if (!in_array($status, ['open', 'closed'], true)) {
        flash('error', ['type' => 'danger', 'message' => 'Transfer window status selection is invalid.']);
        redirect('app/modules/transfers/index.php');
    }

    try {
        $transferSetWindowStatus($windowId, $status);
    } catch (Throwable $exception) {
        flash('error', ['type' => 'danger', 'message' => $exception->getMessage() !== '' ? $exception->getMessage() : 'The transfer window status could not be updated.']);
        redirect('app/modules/transfers/index.php');
    }

    flash('success', ['type' => 'success', 'message' => 'Transfer window status updated successfully.']);
    redirect('app/modules/transfers/index.php');
}

if ($action === 'review_interest') {
    remember_old_input($_POST);

    $errors = [];
    $payload = $validateTransferReviewPayload($_POST, $errors);

    if ($errors !== []) {
        flash('errors', $errors);
        flash('error', ['type' => 'danger', 'message' => 'Please correct the transfer review form errors.']);
        redirect('app/modules/transfers/view.php?id=' . (int) ($payload['transfer_id'] ?? 0));
    }

    try {
        $transferRecordSellingClubDecision($payload);
    } catch (Throwable $exception) {
        flash('error', ['type' => 'danger', 'message' => 'The transfer review decision could not be recorded.']);
        redirect('app/modules/transfers/view.php?id=' . (int) $payload['transfer_id']);
    }

    clear_old_input();
    flash('success', ['type' => 'success', 'message' => 'Transfer interest decision recorded successfully.']);
    redirect('app/modules/transfers/view.php?id=' . (int) $payload['transfer_id']);
}

if ($action === 'submit_offer') {
    remember_old_input($_POST);

    $errors = [];
    $payload = $validateTransferOfferPayload($_POST, $errors);

    if ($errors !== []) {
        flash('errors', $errors);
        flash('error', ['type' => 'danger', 'message' => 'Please correct the transfer offer form errors.']);
        redirect('app/modules/transfers/view.php?id=' . (int) ($payload['transfer_id'] ?? 0));
    }

    try {
        $transferSubmitOffer($payload);
    } catch (Throwable $exception) {
        flash('error', ['type' => 'danger', 'message' => 'The transfer offer could not be submitted.']);
        redirect('app/modules/transfers/view.php?id=' . (int) $payload['transfer_id']);
    }

    clear_old_input();
    flash('success', ['type' => 'success', 'message' => 'Transfer offer submitted successfully.']);
    redirect('app/modules/transfers/view.php?id=' . (int) $payload['transfer_id']);
}

if ($action === 'review_offer') {
    remember_old_input($_POST);

    $errors = [];
    $payload = $validateTransferOfferReviewPayload($_POST, $errors);

    if ($errors !== []) {
        flash('errors', $errors);
        flash('error', ['type' => 'danger', 'message' => 'Please correct the transfer offer review form errors.']);
        redirect('app/modules/transfers/view.php?id=' . (int) ($payload['transfer_id'] ?? 0));
    }

    try {
        $transferRecordOfferDecision($payload);
    } catch (Throwable $exception) {
        flash('error', ['type' => 'danger', 'message' => 'The transfer offer decision could not be recorded.']);
        redirect('app/modules/transfers/view.php?id=' . (int) $payload['transfer_id']);
    }

    clear_old_input();
    flash('success', ['type' => 'success', 'message' => 'Transfer offer decision recorded successfully.']);
    redirect('app/modules/transfers/view.php?id=' . (int) $payload['transfer_id']);
}

if ($action === 'review_approval') {
    remember_old_input($_POST);

    $errors = [];
    $payload = $validateTransferApprovalPayload($_POST, $errors);

    if ($errors !== []) {
        flash('errors', $errors);
        flash('error', ['type' => 'danger', 'message' => 'Please correct the transfer approval form errors.']);
        redirect('app/modules/transfers/view.php?id=' . (int) ($payload['transfer_id'] ?? 0));
    }

    try {
        $transferRecordApprovalDecision($payload);
    } catch (Throwable $exception) {
        flash('error', ['type' => 'danger', 'message' => 'The transfer approval decision could not be recorded.']);
        redirect('app/modules/transfers/view.php?id=' . (int) $payload['transfer_id']);
    }

    clear_old_input();
    flash('success', ['type' => 'success', 'message' => 'Transfer approval decision recorded successfully.']);
    redirect('app/modules/transfers/view.php?id=' . (int) $payload['transfer_id']);
}

if ($action === 'review_medical') {
    remember_old_input($_POST);

    $errors = [];
    $payload = $validateTransferMedicalPayload($_POST, $errors);

    if ($errors !== []) {
        flash('errors', $errors);
        flash('error', ['type' => 'danger', 'message' => 'Please correct the medical completion form errors.']);
        redirect('app/modules/transfers/view.php?id=' . (int) ($payload['transfer_id'] ?? 0));
    }

    try {
        $transferRecordMedicalDecision($payload);
    } catch (Throwable $exception) {
        flash('error', ['type' => 'danger', 'message' => 'The medical decision could not be recorded.']);
        redirect('app/modules/transfers/view.php?id=' . (int) $payload['transfer_id']);
    }

    clear_old_input();
    flash('success', ['type' => 'success', 'message' => 'Medical decision recorded successfully.']);
    redirect('app/modules/transfers/view.php?id=' . (int) $payload['transfer_id']);
}

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