<?php

namespace App\Http\Requests\Admin\Manage;

use App\Concerns\CurrencyStripping;
use App\Enums\OrderChannel;
use App\Enums\PaymentType;
use App\Enums\PriceType;
use App\Enums\ProductStockQuality;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class TransactionRequest extends FormRequest
{
    use CurrencyStripping;

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

    public function prepareForValidation(): void
    {
        $this->merge($this->stripCurrencyDot($this->all(), 'discount', 'nego_price'));
    }

    public function rules(): array
    {
        return [
            'stock_type' => ['sometimes', 'required', Rule::in(ProductStockQuality::values())],
            'channel' => ['sometimes', 'required', Rule::in(OrderChannel::values())],
            'price_type' => ['sometimes', 'required', Rule::in(array_diff(PriceType::values(), [PriceType::CAPITAL->value]))],
            'payment_type' => ['sometimes', 'required', Rule::in(PaymentType::values())],
            'customer_id' => ['nullable', 'integer', Rule::exists('customers', 'id')],
            'marketing_id' => ['nullable', 'integer', Rule::exists('users', 'id')],
            'discount' => ['nullable', 'integer', 'min:0'],
            'nego_price' => ['nullable', 'integer'],
            'is_completed' => ['sometimes', 'boolean'],
            'tiktok_order_id' => ['nullable', 'string', 'max:100'],
            'shopee_order_id' => ['nullable', 'string', 'max:100'],
            'items' => ['required', 'array', 'min:1'],
            'items.*.product_variant_id' => ['required', 'integer', Rule::exists('product_variants', 'id')],
            'items.*.quantity' => ['required', 'integer', 'min:1'],
            'notes' => ['nullable', 'string', 'max:100'],
            'photo_key' => [
                Rule::when(
                    in_array($this->input('payment_type'), [PaymentType::TRANSFER->value, PaymentType::QRIS->value]),
                    ['required', 'string', 'max:500'],
                    ['nullable', 'string', 'max:500'],
                ),
            ],
        ];
    }

    public function attributes(): array
    {
        return [
            'stock_type' => 'tipe stok',
            'channel' => 'channel',
            'price_type' => 'tipe harga',
            'payment_type' => 'tipe pembayaran',
            'customer_id' => 'pelanggan',
            'marketing_id' => 'marketing',
            'discount' => 'diskon',
            'nego_price' => 'harga nego',
            'is_completed' => 'pesanan selesai',
            'tiktok_order_id' => 'id pesanan tiktok',
            'shopee_order_id' => 'id pesanan shopee',
            'items' => 'item produk',
            'items.*.product_variant_id' => 'varian produk',
            'items.*.quantity' => 'jumlah',
            'notes' => 'keterangan',
            'photo_key' => 'foto',
        ];
    }
}
