<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Route;

class GroupReportRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $route = Route::currentRouteName();
        $rules = [
            'fk_site' => ['required', 'integer', 'exists:sy_sites,id'],
            'fk_office' => ['required', 'integer', 'exists:sy_offices,id']
        ];

        $routeRules = [
            'groupreport.show' => [
                'fk_group' => ['required', 'integer', 'exists:sy_groups,id'],
                'reportdate' => ['required', 'date']
            ],
            'groupreport.getGroups' => [
                'reportdate' => ['required', 'date']
            ],
            'groupreport.regenerate' => [
                'id' => ['required', 'integer', 'exists:sy_groups_report,id'],
                'fk_group' => ['required', 'integer', 'exists:sy_groups,id'],
                'reportdate' => ['required', 'date']
            ],
            'groupreport.update' => [
                'id' => ['required', 'integer', 'exists:sy_groups_report,id'],
                'amountdue' => ['required', 'numeric'],
                'amountfailed' => ['required', 'numeric'],
                'subtotalamount' => ['required', 'numeric', 'min:0'],
                'amountrecovered' => ['required', 'numeric', 'min:0'],
                'totalamount' => ['required', 'numeric'],
                'incomingamount' => ['required', 'numeric', 'min:0'],
                'outgoingamount' => ['required', 'numeric', 'min:0'],
                'extraweekamount' => ['required', 'numeric', 'min:0'],
                'totaldeliveryamount' => ['required', 'numeric'],
                'nextamountdue' => ['required', 'numeric'],
                'commissionpercent' => ['required', 'numeric', 'min:0'],
                'commissionamount' => ['required', 'numeric', 'min:0'],
                'nextdate' => ['required', 'date']
            ]
        ];

        return array_merge($rules, $routeRules[$route] ?? []);
    }
}
