<?php

namespace App\Http\Requests\Member;

use Illuminate\Foundation\Http\FormRequest;

// ══════════════════════════════════════════════════════════════════
//  InPractice — StoreShareKnowledgeRequest
//  Location: app/Http/Requests/Member/StoreShareKnowledgeRequest.php
//
//  Validates the Share Knowledge text post form.
//
//  Rules:
//    - hub_id must exist in hubs table
//    - title required, 5–200 chars
//    - body required, 20+ chars
//    - source_url optional — must be a valid URL if present, max 2048 chars
//    - source_label optional — max 50 chars
//    - visibility: all | hub
//    - posted_as_nickname: boolean
//    - tags: optional array, max 5 items, each max 30 chars
// ══════════════════════════════════════════════════════════════════

class StoreShareKnowledgeRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true; // EnsureMember middleware already handles auth
    }

    public function rules(): array
    {
        return [
            'hub_id'             => ['required', 'integer', 'exists:hubs,id'],
            'title'              => ['required', 'string', 'min:5', 'max:200'],
            'body'               => ['required', 'string', 'min:20'],
            'source_url'         => ['nullable', 'url', 'max:2048'],
            'source_label'       => ['nullable', 'string', 'max:50'],
            'visibility'         => ['required', 'in:all,hub'],
            'posted_as_nickname' => ['boolean'],
            'language'           => ['required', 'in:en,ar'],
            'tags'               => ['nullable', 'array', 'max:5'],
            'tags.*'             => ['string', 'max:30'],
        ];
    }

    public function messages(): array
    {
        return [
            'hub_id.required'    => __('common.hub_required'),
            'hub_id.exists'      => __('common.hub_invalid'),
            'title.required'     => __('forum.title_required'),
            'title.min'          => __('forum.title_too_short'),
            'title.max'          => __('forum.title_too_long'),
            'body.required'      => __('forum.body_required'),
            'body.min'           => __('forum.body_too_short'),
            'source_url.url'     => __('forum.source_url_invalid'),
            'source_url.max'     => __('forum.source_url_too_long'),
            'visibility.in'      => __('forum.visibility_invalid'),
        ];
    }
}
