<?php

namespace App\Http\Requests\SiteBriefing;

use App\Models\Tenants\SiteBriefing;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    public function rules(): array
    {
        return [
            // 기본 정보
            'title' => 'required|string|max:200',
            'description' => 'nullable|string|max:5000',

            // 거래처/현장 연결
            'partner_id' => 'nullable|integer|exists:clients,id',
            'site_id' => 'nullable|integer|exists:sites,id',

            // 일정 정보
            'briefing_date' => 'required|date',
            'briefing_time' => 'nullable|string|max:10',
            'briefing_type' => ['nullable', 'string', Rule::in(SiteBriefing::TYPES)],
            'location' => 'nullable|string|max:200',
            'address' => 'nullable|string|max:500',

            // 상태 정보
            'status' => ['nullable', 'string', Rule::in(SiteBriefing::STATUSES)],
            'bid_status' => ['nullable', 'string', Rule::in(SiteBriefing::BID_STATUSES)],
            'bid_date' => 'nullable|date',

            // 참석자 정보 (JSON)
            'attendees' => 'nullable|array',
            'attendees.*.user_id' => 'nullable|integer|exists:users,id',
            'attendees.*.name' => 'required|string|max:50',
            'attendees.*.type' => 'required|string|in:internal,external',
            'attendees.*.company' => 'nullable|string|max:100',
            'attendees.*.phone' => 'nullable|string|max:20',
            'attendees.*.email' => 'nullable|email|max:100',
            'attendees.*.position' => 'nullable|string|max:50',
            'attendance_status' => ['nullable', 'string', Rule::in(SiteBriefing::ATTENDANCE_STATUSES)],

            // 공사 정보
            'site_count' => 'nullable|integer|min:0',
            'construction_start_date' => 'nullable|date',
            'construction_end_date' => 'nullable|date|after_or_equal:construction_start_date',
            'vat_type' => ['nullable', 'string', Rule::in(SiteBriefing::VAT_TYPES)],

            // 프론트엔드 호환성을 위한 필드 매핑
            'project_name' => 'nullable|string|max:200', // title로 매핑
            'work_report' => 'nullable|string|max:5000', // description으로 매핑
        ];
    }

    /**
     * 검증 데이터 준비
     */
    protected function prepareForValidation(): void
    {
        // project_name이 있고 title이 없으면 project_name을 title로 사용
        if ($this->filled('project_name') && ! $this->filled('title')) {
            $this->merge(['title' => $this->project_name]);
        }

        // work_report가 있고 description이 없으면 work_report를 description으로 사용
        if ($this->filled('work_report') && ! $this->filled('description')) {
            $this->merge(['description' => $this->work_report]);
        }
    }

    public function messages(): array
    {
        return [
            'title.required' => __('validation.required', ['attribute' => '현장설명회명']),
            'briefing_date.required' => __('validation.required', ['attribute' => '현장설명회 일자']),
            'construction_end_date.after_or_equal' => '공사 종료일은 시작일 이후여야 합니다.',
        ];
    }
}
