<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;

// ══════════════════════════════════════════════════════════════════
//  InPractice — UpdateCaseRequest
//  Location: app/Http/Requests/Admin/UpdateCaseRequest.php
//
//  Validates fields when editing an existing case.
//  All fields are 'sometimes' — only validated if present in request.
//  This allows partial updates (e.g. adding Arabic translation later).
// ══════════════════════════════════════════════════════════════════

class UpdateCaseRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            // ── Classification ─────────────────────────────────
            'hub_id'        => ['sometimes', 'integer', 'exists:hubs,id'],
            'topic_slug'    => ['sometimes', 'string', 'max:100', 'regex:/^[a-z0-9_]+$/'],
            'topic_name_en' => ['sometimes', 'string', 'max:255'],
            'topic_name_ar' => ['sometimes', 'string', 'max:255'],
            'layer'         => ['sometimes', 'string', 'in:generic,sector_specific'],
            'difficulty'    => ['sometimes', 'string', 'in:beginner,intermediate,advanced'],
            'sector_ids'    => ['sometimes', 'nullable', 'array'],
            'sector_ids.*'  => ['integer', 'exists:sectors,id'],

            // ── English Content ────────────────────────────────
            'title_en'               => ['sometimes', 'string', 'max:255'],
            'subtitle_en'            => ['sometimes', 'nullable', 'string', 'max:255'],
            'situation_en'           => ['sometimes', 'string', 'min:100'],
            'challenge_questions_en' => ['sometimes', 'array', 'min:1'],
            'challenge_questions_en.*' => ['required_with:challenge_questions_en', 'string', 'max:500'],
            'solution_en'            => ['sometimes', 'string', 'min:200'],
            'bigger_lesson_en'       => ['sometimes', 'string', 'min:50'],
            'skills_taught_en'       => ['sometimes', 'array', 'min:1'],
            'skills_taught_en.*'     => ['required_with:skills_taught_en', 'string', 'max:200'],

            // ── Arabic Content ─────────────────────────────────
            'title_ar'               => ['sometimes', 'nullable', 'string', 'max:255'],
            'subtitle_ar'            => ['sometimes', 'nullable', 'string', 'max:255'],
            'situation_ar'           => ['sometimes', 'nullable', 'string'],
            'challenge_questions_ar' => ['sometimes', 'nullable', 'array'],
            'challenge_questions_ar.*' => ['nullable', 'string', 'max:500'],
            'solution_ar'            => ['sometimes', 'nullable', 'string'],
            'bigger_lesson_ar'       => ['sometimes', 'nullable', 'string'],
            'skills_taught_ar'       => ['sometimes', 'nullable', 'array'],
            'skills_taught_ar.*'     => ['nullable', 'string', 'max:200'],

            // ── Optional ───────────────────────────────────────
            'case_data'         => ['sometimes', 'nullable', 'array'],
            'read_time_minutes' => ['sometimes', 'integer', 'min:1', 'max:120'],
            'sort_order'        => ['sometimes', 'integer', 'min:0'],
        ];
    }

    public function messages(): array
    {
        return [
            'hub_id.exists'         => __('cases.hub_not_found'),
            'topic_slug.regex'      => __('cases.topic_slug_invalid'),
            'situation_en.min'      => __('cases.situation_too_short'),
            'solution_en.min'       => __('cases.solution_too_short'),
        ];
    }
}
