<?php

namespace App\Http\Requests\Admin\Manage;

use App\Concerns\CurrencyStripping;
use App\Enums\RawMaterialUnit;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class PurchaseRequest extends FormRequest
{
    use CurrencyStripping;

    public function authorize(): bool
    {
        return $this->user()->can('purchases.create')
            || $this->user()->can('purchases.update');
    }

    public function prepareForValidation(): void
    {
        $this->merge($this->stripCurrencyDot($this->all(), 'variants.*.price', 'discount', 'shipping_cost'));
    }

    public function rules(): array
    {
        return [
            'mode' => ['sometimes', 'required', 'in:new,existing'],
            'name' => [Rule::requiredUnless(fn () => $this->input('mode') === 'existing'), 'string', 'max:200'],
            'unit' => [$this->isMethod('post') ? Rule::requiredUnless(fn () => $this->input('mode') === 'existing') : 'nullable', Rule::in(RawMaterialUnit::values())],
            'variants' => [Rule::requiredUnless(fn () => $this->input('mode') === 'existing'), 'array', 'min:1'],
            'variants.*.variant' => [Rule::requiredUnless(fn () => $this->input('mode') === 'existing'), 'string', 'max:200'],
            'variants.*.price' => [Rule::requiredUnless(fn () => $this->input('mode') === 'existing'), 'integer', 'min:0'],
            'variants.*.stock' => [Rule::requiredUnless(fn () => $this->input('mode') === 'existing'), 'integer', 'min:0'],
            'variants.*.photo_key' => [Rule::requiredUnless(fn () => $this->input('mode') === 'existing'), 'string', 'max:500'],
            'existing_items' => [Rule::requiredIf(fn () => $this->input('mode') === 'existing'), 'array', 'min:1'],
            'existing_items.*.raw_material_price_id' => ['required', 'integer', Rule::exists('raw_material_prices', 'id')],
            'existing_items.*.quantity' => ['required', 'integer', 'min:1'],
            'existing_items.*.unit_price' => ['required', 'integer', 'min:0'],
            'supplier_id' => ['required', 'integer', Rule::exists('suppliers', 'id')],
            'discount' => ['nullable', 'integer', 'min:0'],
            'shipping_cost' => ['nullable', 'integer', 'min:0'],
            'notes' => ['nullable', 'string', 'max:100'],
            'photo_key' => ['nullable', 'string', 'max:500'],
        ];
    }

    public function attributes(): array
    {
        return [
            'name' => 'nama bahan baku',
            'unit' => 'satuan',
            'variants' => 'varian',
            'variants.*.variant' => 'nama varian',
            'variants.*.price' => 'harga',
            'variants.*.stock' => 'stok',
            'variants.*.photo_key' => 'foto varian',
            'existing_items' => 'item bahan baku',
            'existing_items.*.raw_material_price_id' => 'varian bahan baku',
            'existing_items.*.quantity' => 'jumlah',
            'existing_items.*.unit_price' => 'harga beli',
            'supplier_id' => 'supplier',
            'discount' => 'diskon',
            'shipping_cost' => 'ongkir',
            'notes' => 'keterangan',
            'photo_key' => 'foto',
        ];
    }
}
