<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class ReceiveDocumentRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true; // Authorization handled by middleware
    }

    public function rules(): array
    {
        return [
            'document_id' => 'required|string|max:36',
            'original_filename' => 'required|string|max:255',
            'title' => 'required|string|max:500',
            'file_extension' => 'nullable|string|max:20',
            'mime_type' => 'nullable|string|max:100',
            'markdown_content' => 'nullable|string',
            'html_content' => 'nullable|string',
            'content_type' => 'required|in:html,markdown,both',

            // Metadata
            'metadata' => 'required|array',
            'metadata.processed_at' => 'required|date',
            'metadata.file_size' => 'nullable|integer',
            'metadata.chunk_count' => 'required|integer|min:0',
            'metadata.total_pages' => 'nullable|integer|min:0',
            'metadata.processing_duration_seconds' => 'nullable|numeric',

            // Categories
            'jurisdictions' => 'nullable|array',
            'jurisdictions.*.id' => 'required|integer',
            'jurisdictions.*.code' => 'required|string',
            'jurisdictions.*.name' => 'required|string',
            'jurisdictions.*.display_name' => 'required|string',

            'doc_categories' => 'nullable|array',
            'doc_categories.*.id' => 'required|integer',
            'doc_categories.*.code' => 'required|string',
            'doc_categories.*.name' => 'required|string',
            'doc_categories.*.display_name' => 'required|string',

            'law_subjects' => 'nullable|array',
            'law_subjects.*.id' => 'required|integer',
            'law_subjects.*.code' => 'required|string',
            'law_subjects.*.name' => 'required|string',
            'law_subjects.*.display_name' => 'required|string',

            'pathways' => 'nullable|array',
            'pathways.*.id' => 'required|integer',
            'pathways.*.code' => 'required|string',
            'pathways.*.name' => 'required|string',
            'pathways.*.display_name' => 'required|string',

            // Chunks
            'chunks' => 'nullable|array',
            'chunks.*.uuid' => 'required|string|max:36',
            'chunks.*.unique_url' => 'required|string|max:500',
            'chunks.*.chunk_index' => 'required|integer|min:0',
            'chunks.*.chunk_type' => 'required|string|max:20',
            'chunks.*.hierarchy_level' => 'required|integer|min:0',
            'chunks.*.hierarchy_path' => 'nullable|string|max:500',
            'chunks.*.title' => 'nullable|string|max:500',
            'chunks.*.content' => 'required|string',
            'chunks.*.markdown_content' => 'required|string',
            'chunks.*.html_content' => 'nullable|string',
            'chunks.*.token_count' => 'nullable|integer',
            'chunks.*.start_page' => 'nullable|integer',
            'chunks.*.end_page' => 'nullable|integer',
            'chunks.*.chunk_metadata' => 'nullable|array',
        ];
    }

    protected function failedValidation(Validator $validator): void
    {
        throw new HttpResponseException(response()->json([
            'status' => 'error',
            'error_code' => 'VALIDATION_ERROR',
            'message' => 'Validation failed',
            'details' => $validator->errors(),
        ], 400));
    }
}
