<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class CheckInvoiceRule implements ValidationRule
{


    protected $whmcsServices;
    public function __construct($whmcsServices)
    {
        $this->whmcsServices = $whmcsServices;
    }

    /**
     * Run the validation rule.
     *
     * @param  \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $order = $this->whmcsServices->getInvoiceDetailByInvoiceNumber($value);
        if (isset($order['status']) && $order['status'] === 'error') {
            $fail('Invoice Not Found');
        }

        if(isset($order['status']) && $order['status'] !== 'Unpaid'){
            $fail('Invalid Invoice Status');
        }

    }
}
