<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Enums\MembershipStatus;
use App\Models\ImportJob;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreImportRequest extends FormRequest
{
    public function authorize(): bool
    {
        $branchId = $this->filled('branch_id') ? $this->integer('branch_id') : null;

        return $this->user()->can('create', [ImportJob::class, $branchId]);
    }

    public function rules(): array
    {
        return [
            'file' => ['required', 'file', 'mimes:csv,txt', 'max:20480'],
            'branch_id' => ['nullable', 'integer', 'exists:branches,id'],
            'options' => ['nullable', 'array'],
            'options.dedupe' => ['nullable', Rule::in(['none', 'phone_or_email'])],
            'options.default_status' => ['nullable', Rule::enum(MembershipStatus::class)],
        ];
    }
}
