<?php
require_once __DIR__ . '/../../public/vendor/autoload.php';

use App\Models\PaymentRequest;
use App\Models\LoanRepayment;
use App\Models\Loan;
use App\Helpers\Auth;

// Start session and simulate admin login
Auth::start();
$_SESSION['logged_in'] = true;
$_SESSION['user_id'] = 1;
$_SESSION['user_name'] = 'Admin User';
$_SESSION['user_email'] = 'admin@example.com';
$_SESSION['user_role'] = 'admin';

// Create a dummy loan repayment and payment_request
$loanModel = new Loan();
$loanId = $loanModel->create([
    'user_id' => 1,
    'loan_type_id' => 1,
    'amount' => 50.00,
    'interest_rate' => 0.0,
    'duration_months' => 1,
    'total_payable' => 50.00,
    'monthly_payment' => 50.00,
    'status' => 'disbursed',
    'purpose' => 'Resend test'
]);

$repModel = new LoanRepayment();
$repaymentId = $repModel->create([
    'loan_id' => $loanId,
    'installment_number' => 1,
    'due_date' => date('Y-m-d'),
    'amount' => 50.00,
    'paid_amount' => 0.00,
    'status' => 'pending'
]);

$pr = new PaymentRequest();
$prId = $pr->create([
    'user_id' => 1,
    'loan_repayment_id' => $repaymentId,
    'gateway' => 'paystack',
    'reference' => null,
    'authorization_url' => null,
    'amount' => 50.00,
    'status' => 'pending'
]);

echo "Inserted payment_request id={$prId}, repayment_id={$repaymentId}\n";

// Call controller resend
$controller = new App\Controllers\PaymentRequestController();
$controller->resend(['id' => $prId]);

echo "Resend attempted; check session for success/error messages.\n";
if (isset($_SESSION['success'])) echo "SUCCESS: " . $_SESSION['success'] . "\n";
if (isset($_SESSION['error'])) echo "ERROR: " . $_SESSION['error'] . "\n";
