<?php

namespace App\Http\Requests\Billing;

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

class StoreBillingRequest extends FormRequest
{
    public function authorize(): bool
    {
        // Member boleh membuat invoice deposit untuk dirinya sendiri; admin untuk siapa saja.
        return $this->user() !== null;
    }

    /**
     * @return array<string, ValidationRule|array<int, mixed>|string>
     */
    public function rules(): array
    {
        // user_id wajib hanya untuk admin (memilih user lain); member self-target di controller.
        return [
            'jumlah_credit' => ['required', 'integer', 'min:50000'],
            'rate' => ['required', 'integer', 'min:1'],
            'bank_id' => ['nullable', Rule::exists('banks', 'id')],
            'user_id' => ['nullable', Rule::requiredIf(fn () => $this->user()?->isAdmin() ?? false), Rule::exists('users', 'id')],
            'payment_method' => ['nullable', 'string', 'in:manual,duitku'],
        ];
    }
}
