<?php

namespace App\Http\Requests\Admin\Manage;

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

class CuttingRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()->can('cuttings.create')
            || $this->user()->can('cuttings.update');
    }

    public function rules(): array
    {
        return [
            'description' => ['nullable', 'string', 'max:100'],
            'product_name' => ['required', 'string', 'max:255'],
            'sample' => ['required', 'integer'],
            'original_outside_sample' => ['required', 'integer'],
            'cutting_result' => ['required', 'integer', 'min:1'],
            'materials' => ['required', 'array', 'min:1'],
            'materials.*.raw_material_price_id' => ['required', 'integer', Rule::exists('raw_material_prices', 'id')],
            'materials.*.material_usage' => ['required', 'integer', 'min:1'],
            'materials.*.material_result' => ['required', 'integer'],
            'materials.*.combination_index' => ['nullable', 'integer'],
            'combinations' => ['nullable', 'array'],
            'combinations.*.material_result' => ['nullable', 'integer'],
            'photo_key' => ['nullable', 'string', 'max:500'],
        ];
    }

    public function attributes(): array
    {
        return [
            'description' => 'keterangan',
            'product_name' => 'nama produk',
            'sample' => 'sample',
            'original_outside_sample' => 'diluar sample',
            'cutting_result' => 'hasil',
            'materials' => 'bahan baku',
            'materials.*.raw_material_price_id' => 'varian bahan baku',
            'materials.*.material_usage' => 'pemakaian',
            'materials.*.material_result' => 'hasil material',
            'materials.*.combination_index' => 'indeks kombinasi',
            'combinations' => 'kombinasi',
            'combinations.*.material_result' => 'hasil kombinasi',
            'photo_key' => 'foto',
        ];
    }
}
