<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ArchiveSettingRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true; // TODO: add policy / gate later
    }

    public function rules(): array
    {
        $chains = (array) config('archiving.chains', []);
        // Focus entity names (seeded focuses) allowed as primary tables
        $focuses = ['contracts', 'client_cases'];
        $allowed = array_unique(array_merge($focuses, $chains));

        return [
            'action_id' => ['nullable', 'exists:actions,id'],
            'decision_id' => ['nullable', 'exists:decisions,id'],
            'segment_id' => ['nullable', 'exists:segments,id'],
            'entities' => ['required', 'array', 'min:1'],
            'entities.*.table' => ['required', 'string', 'in:'.implode(',', $allowed)],
            'entities.*.related' => ['nullable', 'array'],
            'entities.*.conditions' => ['nullable', 'array'],
            'entities.*.columns' => ['nullable', 'array'],
            'name' => ['nullable', 'string', 'max:255'],
            'description' => ['nullable', 'string'],
            'enabled' => ['boolean'],
            'strategy' => ['required', 'in:immediate,scheduled,queued,manual'],
            'soft' => ['boolean'],
            'reactivate' => ['boolean'],
            'options' => ['nullable', 'array'],
        ];
    }
}
