<?php

namespace Modules\Admin\app\Http\Requests;

use Closure;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Modules\Admin\app\Models\Discount;
use Modules\Admin\app\Models\DiscountType;
use Modules\Admin\app\Rules\PercentRule;
use Modules\Admin\app\Rules\PriceRule;

class DiscountRequest extends FormRequest
{
    protected function checkValue(string $attribute, $value, Closure $fail)
    {
        function checkPrice(string $attribute, $value, $fail)
        {
            if (!PriceRule::check($value)) {
                $fail(PriceRule::message());
            }
        }
        function checkPercent(string $attribute, $value, $fail)
        {
            if (!PercentRule::check($value)) {
                $fail(PercentRule::message());
            }
        }

        if ($this->has('discounts')) {
            foreach ($this->get('discounts') as $discountRequest) {
                if ($discountRequest['discount_type_id'] === 1) {
                    checkPrice($attribute, $value, $fail);
                } else if ($discountRequest['discount_type_id'] === 2) {
                    checkPercent($attribute, $value, $fail);
                } else {
                    $fail("The {$attribute} is invalid.");
                }
            }
        } else {
            if ($this->get('discount_type_id') === 1) {
                checkPrice($attribute, $value, $fail);
            } else if ($this->get('discount_type_id') === 2) {
                checkPercent($attribute, $value, $fail);
            } else {
                $fail("The {$attribute} is invalid.");
            }
        }
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $actionMethod = $this->route()->getActionMethod();
        $rules = [];

        // update single
        if ($actionMethod == 'update') {
            $rules = [
                'id' => 'required|exists:' . (new Discount())->getTable(),
                'discount_type_id' => 'exists:' . (new DiscountType)->getTable() . ',id',
                'value' => function (string $attribute, $value, Closure $fail) {
                    $this->checkValue($attribute, $value, $fail);
                },
            ];
        }
        if ($actionMethod == 'create') {
            $rules = [
                'discount_type_id' => 'exists:' . (new DiscountType)->getTable() . ',id',
                'value' => function (string $attribute, $value, Closure $fail) {
                    $this->checkValue($attribute, $value, $fail);
                },
            ];
        }

        // multiple
        if ($actionMethod == 'updates') {
            $rules = [
                'discounts.*.id' => 'required|exists:' . (new Discount())->getTable(),
                'discounts.*.discount_type_id' => 'exists:' . (new DiscountType)->getTable() . ',id',
                'discounts.*.value' => function (string $attribute, $value, Closure $fail) {
                    $this->checkValue($attribute, $value, $fail);
                },
            ];
        }
        if ($actionMethod == 'creates') {
            $rules = [
                'discounts.*.id' => 'required|exists:' . (new Discount())->getTable(),
                'discounts.*.discount_type_id' => 'exists:' . (new DiscountType)->getTable() . ',id',
                'discounts.*.value' => function (string $attribute, $value, Closure $fail) {
                    $this->checkValue($attribute, $value, $fail);
                },
            ];
        }

        // single
        if ($actionMethod == 'delete') {
            $rules = [
                'id' => 'required|exists:' . (new Discount())->getTable()
            ];
        }

        // multiple
        if ($actionMethod == 'deletes') {
            $rules = [
                'discounts.*.id' => 'required|exists:' . (new Discount())->getTable()
            ];
        }

        return array_merge([
            'status' => 'in:0,1,true,false',
            'active_date' => 'size:10', // as timestamp unix
            'expiry' => 'size:10', // as timestamp unix
            'date_used' => 'size:10', // as timestamp unix
            'discounts.*.status' => 'in:0,1,true,false',
        ], $rules);
    }

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }
}
