<?php

namespace App\Http\Requests\Tenant;

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

class TaskStoreRequest 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' => ['required', 'string', 'max:255'],
            'description' => ['nullable', 'string'],
            'collection_id' => ['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' => ['nullable', Rule::in(['low', 'medium', 'high', 'urgent'])],
            'due_date' => ['nullable', 'date', 'after:now'],
            'is_completed' => ['nullable', 'boolean'],
            'assigned_to' => ['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' => ['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.',
            'due_date.after' => 'Das Fälligkeitsdatum muss in der Zukunft liegen.',
            'assigned_to.exists' => 'Der zugewiesene Benutzer existiert nicht.',
        ];
    }
}
