<?php

namespace App\Http\Requests\Admin;

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

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

    public function rules(): array
    {
        $id = $this->route('produk') ?? $this->input('id');

        return [
            'category_id'      => ['required', 'exists:categories,id'],
            'partner_id'       => ['required', 'exists:partners,id'],
            'name'             => ['required', 'string', 'max:200'],
            'slug'             => ['nullable', 'string', 'max:220', Rule::unique('products', 'slug')->ignore($id)->whereNull('deleted_at')],
            'description'      => ['nullable', 'string', 'max:5000'],
            'buy_price'        => ['required', 'numeric', 'min:0'],
            'sell_price'       => ['required', 'numeric', 'min:0'],
            'unit'             => ['required', 'string', 'max:50'],
            'image'            => ['nullable', 'image', 'mimes:jpg,jpeg,png,webp', 'max:3072'],
            'commission_type'  => ['nullable', Rule::in(['percent', 'fixed'])],
            'commission_value' => ['nullable', 'numeric', 'min:0'],
            'is_active'        => ['boolean'],
            'is_featured'      => ['boolean'],
            'sort_order'       => ['integer', 'min:0', 'max:9999'],
        ];
    }

    public function messages(): array
    {
        return [
            'category_id.required' => 'Kategori wajib dipilih.',
            'category_id.exists'   => 'Kategori tidak valid.',
            'partner_id.required'  => 'Mitra wajib dipilih.',
            'partner_id.exists'    => 'Mitra tidak valid.',
            'name.required'        => 'Nama produk wajib diisi.',
            'buy_price.required'   => 'Harga beli wajib diisi.',
            'sell_price.required'  => 'Harga jual wajib diisi.',
            'unit.required'        => 'Satuan wajib diisi.',
            'image.image'          => 'File harus berupa gambar.',
            'image.max'            => 'Ukuran gambar maksimal 3MB.',
        ];
    }
}
