<?php

namespace App\Http\Requests\Construction;

use App\Models\Construction\HandoverReport;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    public function rules(): array
    {
        return [
            // 기본 정보
            'report_number' => 'sometimes|string|max:50',
            'contract_id' => 'nullable|integer',
            'site_name' => 'sometimes|string|max:255',

            // 거래처 정보
            'partner_id' => 'nullable|integer',
            'partner_name' => 'nullable|string|max:255',

            // 담당자 정보
            'contract_manager_id' => 'nullable|integer',
            'contract_manager_name' => 'nullable|string|max:100',
            'construction_pm_id' => 'nullable|integer',
            'construction_pm_name' => 'nullable|string|max:100',

            // 계약 상세
            'total_sites' => 'nullable|integer|min:0',
            'contract_amount' => 'nullable|numeric|min:0',
            'contract_date' => 'nullable|date',
            'contract_start_date' => 'nullable|date',
            'contract_end_date' => 'nullable|date|after_or_equal:contract_start_date',
            'completion_date' => 'nullable|date',

            // 상태 정보
            'status' => [
                'nullable',
                Rule::in([HandoverReport::STATUS_PENDING, HandoverReport::STATUS_COMPLETED]),
            ],

            // 2차 배관
            'has_secondary_piping' => 'nullable|boolean',
            'secondary_piping_amount' => 'nullable|numeric|min:0',
            'secondary_piping_note' => 'nullable|string',

            // 도장 & 코킹
            'has_coating' => 'nullable|boolean',
            'coating_amount' => 'nullable|numeric|min:0',
            'coating_note' => 'nullable|string',

            // 장비 외 실행금액
            'external_equipment_cost' => 'nullable|array',
            'external_equipment_cost.shipping_cost' => 'nullable|numeric|min:0',
            'external_equipment_cost.high_altitude_work' => 'nullable|numeric|min:0',
            'external_equipment_cost.public_expense' => 'nullable|numeric|min:0',

            // 특이사항
            'special_notes' => 'nullable|string',

            // 공사담당자 목록
            'managers' => 'nullable|array',
            'managers.*.name' => 'required_with:managers|string|max:100',
            'managers.*.non_performance_reason' => 'nullable|string',
            'managers.*.signature' => 'nullable|string',

            // 계약 ITEM 목록
            'items' => 'nullable|array',
            'items.*.item_no' => 'nullable|integer|min:0',
            'items.*.name' => 'required_with:items|string|max:255',
            'items.*.product' => 'nullable|string|max:255',
            'items.*.quantity' => 'nullable|integer|min:0',
            'items.*.remark' => 'nullable|string',

            // 기타
            'is_active' => 'nullable|boolean',
        ];
    }

    public function messages(): array
    {
        return [
            'report_number.max' => __('validation.max.string', ['attribute' => '보고서번호', 'max' => 50]),
            'contract_end_date.after_or_equal' => __('validation.after_or_equal', ['attribute' => '계약종료일', 'date' => '계약시작일']),
            'managers.*.name.required_with' => __('validation.required', ['attribute' => '담당자명']),
            'items.*.name.required_with' => __('validation.required', ['attribute' => 'ITEM 명칭']),
        ];
    }
}
