<?php

namespace App\Http\Requests\Api\Service;

use App\Models\Service;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ServiceRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $service = Service::find($this->service_id);

        return [
            'service_id' => 'required|exists:services,id',
            'power_id' => [
                Rule::requiredIf(function () use ($service) {
                    // If service exists and is_power is true, require power_id
                    return $service && $service->is_power;
                }),
                'nullable', // Allow null if the condition is false
                'exists:powers,id' // Validate the power_id exists in the powers table
            ],
            'city_id' => 'required|exists:cities,id',
            'phone' => 'required|numeric',
            'full_name' => 'required',
            'comment' => 'nullable',
        ];
    }
}
