<?php

namespace App\Http\Requests;

use App\Models\Plan;
use Illuminate\Validation\Rule;

class UpdatePlanRequest extends StorePlanRequest
{
    protected function prepareForValidation(): void
    {
        $plan = $this->route('plan');

        $this->merge([
            'slug' => $this->filled('slug')
                ? str($this->input('slug'))->slug()->toString()
                : Plan::uniqueSlug((string) $this->input('name'), $plan?->id),
            'invoice_module_enabled' => $this->boolean('invoice_module_enabled'),
            'whatsapp_reminder_enabled' => $this->boolean('whatsapp_reminder_enabled'),
            'email_reminder_enabled' => $this->boolean('email_reminder_enabled'),
            'reports_enabled' => $this->boolean('reports_enabled'),
            'custom_branding_enabled' => $this->boolean('custom_branding_enabled'),
        ]);
    }

    public function rules(): array
    {
        $plan = $this->route('plan');
        $rules = parent::rules();
        $rules['slug'] = ['required', 'alpha_dash', 'max:140', Rule::unique('plans', 'slug')->ignore($plan?->id)];

        return $rules;
    }
}
