<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ImportTranslationsRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->role === 'business_owner' || $this->user()?->role === 'super_admin';
    }

    public function rules(): array
    {
        return [
            'language_id' => ['required', 'exists:languages,id'],
            'json_payload' => ['nullable', 'string'],
            'translation_file' => ['nullable', 'file', 'mimes:json,txt', 'max:2048'],
        ];
    }

    public function payload(): array
    {
        $json = $this->input('json_payload');

        if ($this->hasFile('translation_file')) {
            $json = file_get_contents($this->file('translation_file')->getRealPath());
        }

        return json_decode((string) $json, true) ?: [];
    }
}
