<?php

namespace App\Http\Requests\Quote;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    /**
     * 프론트엔드 필드명을 백엔드 필드명으로 정규화
     * manager_name → manager, manager_contact → contact
     */
    protected function prepareForValidation(): void
    {
        $mappings = [
            'manager_name' => 'manager',
            'manager_contact' => 'contact',
        ];

        foreach ($mappings as $from => $to) {
            if ($this->has($from) && ! $this->has($to)) {
                $this->merge([$to => $this->input($from)]);
            }
        }
    }

    public function rules(): array
    {
        return [
            // 기본 정보
            'receipt_date' => 'nullable|date',
            'author' => 'nullable|string|max:50',

            // 발주처 정보
            'client_id' => 'nullable|integer',
            'client_name' => 'nullable|string|max:100',
            'manager' => 'nullable|string|max:50',
            'contact' => 'nullable|string|max:50',

            // 현장 정보
            'site_id' => 'nullable|integer',
            'site_name' => 'nullable|string|max:100',
            'site_code' => 'nullable|string|max:50',

            // 제품 정보
            'product_category' => [
                'nullable',
                Rule::exists('common_codes', 'code')->where('code_group', 'product_category'),
            ],
            'product_id' => 'nullable|integer',
            'product_code' => 'nullable|string|max:50',
            'product_name' => 'nullable|string|max:100',

            // 규격 정보
            'open_size_width' => 'nullable|numeric|min:0',
            'open_size_height' => 'nullable|numeric|min:0',
            'quantity' => 'nullable|integer|min:1',
            'unit_symbol' => 'nullable|string|max:10',
            'floors' => 'nullable|string|max:50',

            // 금액 정보
            'material_cost' => 'nullable|numeric|min:0',
            'labor_cost' => 'nullable|numeric|min:0',
            'install_cost' => 'nullable|numeric|min:0',
            'discount_rate' => 'nullable|numeric|min:0|max:100',
            'discount_amount' => 'nullable|numeric|min:0',
            'total_amount' => 'nullable|numeric|min:0',

            // 기타 정보
            'completion_date' => 'nullable|date',
            'remarks' => 'nullable|string|max:500',
            'memo' => 'nullable|string',
            'notes' => 'nullable|string',

            // 자동산출 입력값
            'calculation_inputs' => 'nullable|array',
            'calculation_inputs.*' => 'nullable',

            // 견적 옵션 (summary_items, expense_items, price_adjustments)
            'options' => 'nullable|array',
            // 견적 요약 정보
            'options.summary_items' => 'nullable|array',
            'options.summary_items.*.id' => 'required|string',
            'options.summary_items.*.name' => 'required|string|max:100',
            'options.summary_items.*.quantity' => 'nullable|numeric|min:0',
            'options.summary_items.*.unit' => 'nullable|string|max:20',
            'options.summary_items.*.material_cost' => 'nullable|numeric|min:0',
            'options.summary_items.*.labor_cost' => 'nullable|numeric|min:0',
            'options.summary_items.*.total_cost' => 'nullable|numeric|min:0',
            'options.summary_items.*.remarks' => 'nullable|string|max:500',
            // 공과 상세
            'options.expense_items' => 'nullable|array',
            'options.expense_items.*.id' => 'required|string',
            'options.expense_items.*.name' => 'required|string|max:100',
            'options.expense_items.*.amount' => 'nullable|numeric|min:0',
            // 단가 조정 (레거시)
            'options.price_adjustments' => 'nullable|array',
            'options.price_adjustments.*.id' => 'nullable|string',
            'options.price_adjustments.*.category' => 'nullable|string|max:50',
            'options.price_adjustments.*.unit_price' => 'nullable|numeric|min:0',
            'options.price_adjustments.*.coating' => 'nullable|numeric|min:0',
            'options.price_adjustments.*.batting' => 'nullable|numeric|min:0',
            'options.price_adjustments.*.box_reinforce' => 'nullable|numeric|min:0',
            'options.price_adjustments.*.painting' => 'nullable|numeric|min:0',
            'options.price_adjustments.*.total' => 'nullable|numeric|min:0',

            // 품목 단가 조정 (신규 구조)
            'options.price_adjustment_data' => 'nullable|array',
            'options.price_adjustment_data.caulking' => 'nullable|array',
            'options.price_adjustment_data.rail' => 'nullable|array',
            'options.price_adjustment_data.bottom' => 'nullable|array',
            'options.price_adjustment_data.boxReinforce' => 'nullable|array',
            'options.price_adjustment_data.shaft' => 'nullable|array',
            'options.price_adjustment_data.painting' => 'nullable|array',
            'options.price_adjustment_data.motor' => 'nullable|array',
            'options.price_adjustment_data.controller' => 'nullable|array',

            // 견적 상세 항목 (detail_items)
            'options.detail_items' => 'nullable|array',
            'options.detail_items.*.id' => 'required|string',
            'options.detail_items.*.no' => 'nullable|integer',
            'options.detail_items.*.name' => 'nullable|string|max:100',
            'options.detail_items.*.material' => 'nullable|string|max:100',
            'options.detail_items.*.width' => 'nullable|numeric|min:0',
            'options.detail_items.*.height' => 'nullable|numeric|min:0',
            'options.detail_items.*.quantity' => 'nullable|numeric|min:0',
            'options.detail_items.*.box' => 'nullable|numeric|min:0',
            'options.detail_items.*.assembly' => 'nullable|numeric|min:0',
            'options.detail_items.*.coating' => 'nullable|numeric|min:0',
            'options.detail_items.*.batting' => 'nullable|numeric|min:0',
            'options.detail_items.*.mounting' => 'nullable|numeric|min:0',
            'options.detail_items.*.fitting' => 'nullable|numeric|min:0',
            'options.detail_items.*.controller' => 'nullable|numeric|min:0',
            'options.detail_items.*.width_construction' => 'nullable|numeric|min:0',
            'options.detail_items.*.height_construction' => 'nullable|numeric|min:0',
            'options.detail_items.*.material_cost' => 'nullable|numeric|min:0',
            'options.detail_items.*.labor_cost' => 'nullable|numeric|min:0',
            'options.detail_items.*.quantity_price' => 'nullable|numeric|min:0',
            'options.detail_items.*.expense_quantity' => 'nullable|numeric|min:0',
            'options.detail_items.*.expense_total' => 'nullable|numeric|min:0',
            'options.detail_items.*.total_cost' => 'nullable|numeric|min:0',
            'options.detail_items.*.other_cost' => 'nullable|numeric|min:0',
            'options.detail_items.*.margin_cost' => 'nullable|numeric|min:0',
            'options.detail_items.*.total_price' => 'nullable|numeric|min:0',
            'options.detail_items.*.unit_price' => 'nullable|numeric|min:0',
            'options.detail_items.*.expense' => 'nullable|numeric|min:0',
            'options.detail_items.*.margin_rate' => 'nullable|numeric|min:0',
            'options.detail_items.*.unit_quantity' => 'nullable|numeric|min:0',
            'options.detail_items.*.expense_result' => 'nullable|numeric|min:0',
            'options.detail_items.*.margin_actual' => 'nullable|numeric|min:0',
            // 계산 필드
            'options.detail_items.*.calc_weight' => 'nullable|numeric',
            'options.detail_items.*.calc_area' => 'nullable|numeric',
            'options.detail_items.*.calc_steel_screen' => 'nullable|numeric',
            'options.detail_items.*.calc_caulking' => 'nullable|numeric',
            'options.detail_items.*.calc_rail' => 'nullable|numeric',
            'options.detail_items.*.calc_bottom' => 'nullable|numeric',
            'options.detail_items.*.calc_box_reinforce' => 'nullable|numeric',
            'options.detail_items.*.calc_shaft' => 'nullable|numeric',
            'options.detail_items.*.calc_unit_price' => 'nullable|numeric',
            'options.detail_items.*.calc_expense' => 'nullable|numeric',
            // 조정단가 필드
            'options.detail_items.*.adjusted_caulking' => 'nullable|numeric',
            'options.detail_items.*.adjusted_rail' => 'nullable|numeric',
            'options.detail_items.*.adjusted_bottom' => 'nullable|numeric',
            'options.detail_items.*.adjusted_box_reinforce' => 'nullable|numeric',
            'options.detail_items.*.adjusted_shaft' => 'nullable|numeric',
            'options.detail_items.*.adjusted_painting' => 'nullable|numeric',
            'options.detail_items.*.adjusted_motor' => 'nullable|numeric',
            'options.detail_items.*.adjusted_controller' => 'nullable|numeric',

            // 품목 배열 (전체 교체)
            'items' => 'nullable|array',
            'items.*.item_id' => 'nullable|integer',
            'items.*.item_code' => 'nullable|string|max:50',
            'items.*.item_name' => 'nullable|string|max:100',
            'items.*.specification' => 'nullable|string|max:200',
            'items.*.unit' => 'nullable|string|max:20',
            'items.*.base_quantity' => 'nullable|numeric|min:0',
            'items.*.calculated_quantity' => 'nullable|numeric|min:0',
            'items.*.unit_price' => 'nullable|numeric|min:0',
            'items.*.total_price' => 'nullable|numeric|min:0',
            'items.*.formula' => 'nullable|string|max:500',
            'items.*.formula_result' => 'nullable|string|max:200',
            'items.*.formula_source' => 'nullable|string|max:100',
            'items.*.formula_category' => 'nullable|string|max:50',
            'items.*.data_source' => 'nullable|string|max:100',
            'items.*.delivery_date' => 'nullable|date',
            'items.*.note' => 'nullable|string|max:500',
            'items.*.sort_order' => 'nullable|integer|min:0',
        ];
    }
}
