<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;

class UpdateProposalRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    // public function authorize(): bool
    // {
    //     return auth()->check() &&
    //           ($this->proposal->created_by === auth()->id() ||
    //             in_array(auth()->user()->role->slug, ['vp', 'sp'], true));
    // }
    
    public function authorize(): bool
    {
        return auth()->check() &&
               ($this->proposal->created_by === auth()->id() ||
                in_array(auth()->user()->role->slug, ['sp', 'vp', 'sd'], true));
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'salutation' => ['nullable', 'string', 'max:20'],
            'contact_person' => ['nullable', 'string', 'max:255'],
            'email' => ['nullable', 'email', 'max:255'],
            'phone' => ['nullable', 'string', 'max:30'],
            'alternate_phone' => ['nullable', 'string', 'max:30'],
            'gst_number' => ['nullable', 'string', 'max:30'],
            'billing_address1' => ['nullable', 'string', 'max:255'],
            'billing_country_id' => ['nullable', 'exists:countries,id'],
            'billing_state_id' => ['nullable', 'exists:proposal_address_states,id'],
            'billing_district_id' => ['nullable', 'exists:proposal_address_districts,id'],
            'billing_city' => ['nullable', 'string', 'max:255'],
            'billing_city_id' => ['nullable', 'string', 'max:255'],
            'billing_area' => ['nullable', 'string', 'max:255'],
            'billing_landmark' => ['nullable', 'string', 'max:255'],
            'billing_pincode' => ['nullable', 'string', 'max:20'],
            'site_address1' => ['nullable', 'string', 'max:255'],
            'site_country_id' => ['nullable', 'exists:countries,id'],
            'site_state_id' => ['nullable', 'exists:proposal_address_states,id'],
            'site_district_id' => ['nullable', 'exists:proposal_address_districts,id'],
            'site_city' => ['nullable', 'string', 'max:255'],
            'site_city_id' => ['nullable', 'string', 'max:255'],
            'site_area' => ['nullable', 'string', 'max:255'],
            'site_landmark' => ['nullable', 'string', 'max:255'],
            'product_id' => ['sometimes', 'exists:lift_products,id'],
            'no_of_floor' => ['nullable', 'integer'],
            'motor_type' => ['nullable', 'in:single_phase,3_phase'],
            'requested_price' => ['sometimes', 'numeric', 'min:0'],
            'original_price' => ['nullable', 'numeric', 'min:0'],
            'discount_percentage' => ['nullable', 'numeric', 'min:0', 'max:100'],
            'discount_amount' => ['nullable', 'numeric', 'min:0'],
            'gst_percentage' => ['sometimes', 'numeric', 'min:0', 'max:100'],
            'installation_cost' => ['nullable', 'numeric', 'min:0'],
            'transport_cost' => ['nullable', 'numeric', 'min:0'],
            'unloading_cost' => ['nullable', 'numeric', 'min:0'],
            'luxe_package' => ['nullable', 'in:yes,no'],
            'upgradation_ids' => ['nullable', 'array'],
            'upgradation_ids.*' => ['nullable', 'exists:upgradations,id'],
            'upgradation_group' => ['nullable', 'array'],
            'upgradation_group.*' => ['nullable'],
            'luxe_package_id' => ['nullable', 'exists:upgradations,id'],
            'door_id' => ['nullable', 'exists:upgradations,id'],
            'door_count' => ['nullable', 'integer', 'min:0', 'max:10'],
            'color_id' => ['nullable', 'exists:upgradations,id'],
            'swing_id' => ['nullable', 'exists:upgradations,id'],
            'cop_id' => ['nullable', 'exists:upgradations,id'],
            'extra_travel_height_id' => ['nullable', 'exists:upgradations,id'],
            'sos_id' => ['nullable', 'exists:upgradations,id'],
            'warranty_type' => ['nullable', 'string', 'max:255'],
            'warranty_years' => ['nullable', 'integer', 'min:0'],
            'amc_type' => ['nullable', 'string', 'max:255'],
            'amc_percentage' => ['nullable', 'numeric', 'min:0'],
            'delivery_type' => ['nullable', 'string', 'max:255'],
            'delivery_term' => ['nullable', 'string', 'max:255'],
            'delivery_amount' => ['nullable', 'numeric', 'min:0'],
            'payment_terms_preset' => ['nullable', 'string', 'max:255'],
            'payment_terms' => ['nullable', 'string'],
            'delivery_terms' => ['nullable', 'string'],
            'installation_notes' => ['nullable', 'string'],
            'terms_and_conditions' => ['nullable', 'string'],
            'valid_until' => ['nullable', 'date'],
        ];
    }
}
