<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;

// ══════════════════════════════════════════════════════════════════
//  InPractice — StoreCaseRequest
//  Location: app/Http/Requests/Admin/StoreCaseRequest.php
//
//  Validates all fields required to create a new case.
//
//  Rules:
//    - English content is always required (source of truth)
//    - Arabic content is always nullable (can be added later)
//    - hub_id must exist in the hubs table
//    - topic_slug is snake_case (e.g. cash_flow, budgeting)
//    - challenge_questions and skills_taught are arrays of strings
//    - case_data is nullable JSON
//    - Only admins can create cases — enforced by EnsureAdmin middleware
//      on the route, not repeated here
// ══════════════════════════════════════════════════════════════════

class StoreCaseRequest extends FormRequest
{
    /**
     * Only admins reach this request — middleware handles it.
     */
    public function authorize(): bool
    {
        return true;
    }

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

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

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

            // ── Optional ───────────────────────────────────────
            'case_data'          => ['nullable', 'array'],
            'read_time_minutes'  => ['nullable', 'integer', 'min:1', 'max:120'],
            'sort_order'         => ['nullable', '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'),
            'challenge_questions_en.required'      => __('cases.questions_required'),
            'skills_taught_en.required'            => __('cases.skills_required'),
        ];
    }
}
