<?php

namespace Modules\Admin\app\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Modules\Admin\app\Models\Package;
use Modules\Admin\app\Rules\PointRule;
use Modules\Admin\app\Rules\PriceRule;

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

        // single
        if ($actionMethod == 'update') {
            $rules = [
                'id' => 'required|exists:' . (new Package())->getTable(),
                'point' => new PointRule,
                'price' => new PriceRule,
                'title' => 'max:127', // from paypal
                'description' => 'max:127'
            ];
        }

        if ($actionMethod == 'create') {
            $rules = [
                'point' => new PointRule,
                'price' => new PriceRule
            ];
        }

        // multiple
        if ($actionMethod == 'updates') {
            $rules = [
                'packages.*.id' => 'required|exists:' . (new Package())->getTable(),
                'packages.*.point' => new PointRule,
                'packages.*.price' => new PriceRule,
                'packages.*.title' => 'max:127',
                'packages.*.description' => 'max:127',
            ];
        }

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

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

        return array_merge([
            'status' => 'in:0,1',
            'packages.*.status' => 'in:0,1',
        ], $rules);
    }

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