<?php

namespace App\Http\Requests\Tenant;

use App\Support\TenantRequestResolver;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class TaskUpdateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
     */
    public function rules(): array
    {
        $tenantId = TenantRequestResolver::resolve($this)->id;

        return [
            'title' => ['sometimes', 'required', 'string', 'max:255'],
            'description' => ['sometimes', 'nullable', 'string'],
            'collection_id' => ['sometimes', 'nullable', 'exists:task_collections,id', function ($attribute, $value, $fail) use ($tenantId) {
                $accessible = \App\Models\TaskCollection::where('id', $value)
                    ->where(function ($query) use ($tenantId) {
                        $query->whereNull('tenant_id');

                        if ($tenantId) {
                            $query->orWhere('tenant_id', $tenantId);
                        }
                    })
                    ->exists();

                if (! $accessible) {
                    $fail('Die TaskCollection gehört nicht zu diesem Tenant.');
                }
            }],
            'priority' => ['sometimes', 'nullable', Rule::in(['low', 'medium', 'high', 'urgent'])],
            'due_date' => ['sometimes', 'nullable', 'date'],
            'is_completed' => ['sometimes', 'boolean'],
            'assigned_to' => ['sometimes', 'nullable', 'exists:users,id', function ($attribute, $value, $fail) use ($tenantId) {
                if ($tenantId && ! \App\Models\User::where('id', $value)->where('tenant_id', $tenantId)->exists()) {
                    $fail('Der Benutzer gehört nicht zu diesem Tenant.');
                }
            }],
            'emotion_id' => ['sometimes', 'nullable', 'exists:emotions,id', function ($attribute, $value, $fail) use ($tenantId) {
                $accessible = \App\Models\Emotion::where('id', $value)
                    ->where(function ($query) use ($tenantId) {
                        $query->whereNull('tenant_id');

                        if ($tenantId) {
                            $query->orWhere('tenant_id', $tenantId);
                        }
                    })
                    ->exists();

                if (! $accessible) {
                    $fail('Die Emotion gehört nicht zu diesem Tenant.');
                }
            }],
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'title.required' => 'Der Task-Titel ist erforderlich.',
            'title.max' => 'Der Task-Titel darf maximal 255 Zeichen haben.',
            'collection_id.exists' => 'Die ausgewählte TaskCollection existiert nicht.',
            'priority.in' => 'Die Priorität muss low, medium, high oder urgent sein.',
            'assigned_to.exists' => 'Der zugewiesene Benutzer existiert nicht.',
        ];
    }
}
