<?php

namespace App\Http\Requests\WorkOrder;

use App\Models\Production\WorkOrder;
use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        return [
            // 기본 정보
            'sales_order_id' => 'nullable|integer|exists:orders,id',
            'project_name' => 'nullable|string|max:200',
            'process_id' => 'nullable|integer|exists:processes,id',
            'status' => ['nullable', 'in:'.implode(',', WorkOrder::STATUSES)],
            'assignee_id' => 'nullable|integer|exists:users,id',
            'assignee_ids' => 'nullable|array',
            'assignee_ids.*' => 'integer|exists:users,id',
            'team_id' => 'nullable|integer|exists:departments,id',
            'scheduled_date' => 'nullable|date',
            'priority' => 'nullable|integer|min:1|max:9',
            'memo' => 'nullable|string',
            'is_active' => 'nullable|boolean',

            // 품목 배열 (있으면 전체 교체)
            'items' => 'nullable|array',
            'items.*.item_id' => 'nullable|integer|exists:items,id',
            'items.*.item_name' => 'required|string|max:200',
            'items.*.specification' => 'nullable|string|max:500',
            'items.*.quantity' => 'nullable|numeric|min:0',
            'items.*.unit' => 'nullable|string|max:20',

            // 벤딩 상세
            'bending_detail' => 'nullable|array',
            'bending_detail.shaft_cutting' => 'nullable|boolean',
            'bending_detail.bearing' => 'nullable|boolean',
            'bending_detail.shaft_welding' => 'nullable|boolean',
            'bending_detail.assembly' => 'nullable|boolean',
            'bending_detail.winder_welding' => 'nullable|boolean',
            'bending_detail.frame_assembly' => 'nullable|boolean',
            'bending_detail.bundle_assembly' => 'nullable|boolean',
            'bending_detail.motor_assembly' => 'nullable|boolean',
            'bending_detail.bracket_assembly' => 'nullable|boolean',
        ];
    }
}
