<?php

namespace App\Http\Requests\Admin\Manage;

use App\Enums\Permission;
use App\Http\Requests\Concerns\ValidatesMediaUploads;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class CuttingRequest extends FormRequest
{
    use ValidatesMediaUploads;

    public function authorize(): bool
    {
        $permission = $this->isMethod('POST')
            ? Permission::CUTTINGS_CREATE
            : Permission::CUTTINGS_UPDATE;

        return $this->user()?->can($permission->value) ?? false;
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        $isCreate = $this->isMethod('POST');

        $rules = [
            'description' => ['nullable', 'string', 'max:100'],

            'sewing_cost' => ['nullable', 'integer', 'min:0'],
            'other_cost' => ['nullable', 'integer', 'min:0'],
        ];

        if (! $isCreate) {
            $rules['materials'] = ['required', 'array', 'min:1'];
            $rules['materials.*.raw_material_price_id'] = [
                'required',
                'integer',
                Rule::exists('raw_material_prices', 'id')->whereNull('deleted_at'),
            ];
            $rules['materials.*.material_usage'] = ['required', 'numeric', 'decimal:0,4', 'gt:0'];
            $rules['materials.*.material_result'] = ['nullable', 'integer', 'min:0'];
            $rules['materials.*.combination_id'] = ['nullable', 'integer'];
            $rules['materials.*.combination_material_result'] = ['nullable', 'integer', 'min:0'];

            $rules['results'] = ['required', 'array', 'min:1'];
            $rules['results.*.product_variant_id'] = [
                'required',
                'integer',
                'distinct',
                Rule::exists('product_variants', 'id')->whereNull('deleted_at'),
            ];
            $rules['results.*.cutting_result'] = ['required', 'integer', 'min:1'];
            $rules['results.*.sample'] = ['required', 'integer', 'min:0'];
            $rules['results.*.original_outside_sample'] = ['required', 'integer', 'min:0'];
        }

        return array_merge(
            $rules,
            $this->photoRules('photos', 10),
        );
    }

    /**
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return array_merge(
            [
                'description' => 'Keterangan',
                'materials' => 'Bahan Baku',
                'materials.*.raw_material_price_id' => 'Bahan Baku',
                'materials.*.material_usage' => 'Pemakaian',
                'materials.*.material_result' => 'Hasil',
                'materials.*.combination_material_result' => 'Hasil Kombinasi',
                'results' => 'Hasil Produk',
                'results.*.product_variant_id' => 'Varian Produk',
                'results.*.cutting_result' => 'Hasil',
                'results.*.sample' => 'sample',
                'results.*.original_outside_sample' => 'Diluar sample',
                'sewing_cost' => 'Jasa Jahit',
                'other_cost' => 'Biaya Lainnya',
            ],
            $this->photoUploadAttributes('Foto Cutting'),
        );
    }
}
