<?php

namespace App\Livewire\Financial;

use App\Domain\Financial\Enums\ExpenseCategory;
use App\Domain\Financial\Services\ExpenseService;
use App\Domain\Shared\Exceptions\DomainException;
use App\Domain\Shared\Traits\UsesBranchScope;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Layout('layouts.app')]
#[Title('تسجيل مصروف')]
class ExpenseForm extends Component
{
    use UsesBranchScope;

    public string $category = '';
    public string $amount_display = '';
    public string $description = '';
    public string $recipient_name = '';
    public string $payment_method = 'cash';
    public string $receipt_reference = '';
    public ?string $expense_date = null;
    public string $notes = '';

    public function mount(): void
    {
        $this->authorize('expenses.create');
        $this->expense_date = now()->toDateString();
    }

    public function rules(): array
    {
        return [
            'category' => 'required|in:' . implode(',', array_column(ExpenseCategory::cases(), 'value')),
            'amount_display' => 'required|numeric|min:0.01',
            'description' => 'required|string|max:500',
            'recipient_name' => 'nullable|string|max:255',
            'payment_method' => 'required|in:cash,card,bank_transfer,wallet,online,cheque,other',
            'receipt_reference' => 'nullable|string|max:100',
            'expense_date' => 'required|date',
            'notes' => 'nullable|string',
        ];
    }

    public function messages(): array
    {
        return [
            'category.required' => 'اختر فئة المصروف',
            'amount_display.required' => 'المبلغ مطلوب',
            'amount_display.min' => 'المبلغ يجب أن يكون أكبر من صفر',
            'description.required' => 'وصف المصروف مطلوب',
            'description.max' => 'الوصف يجب ألا يتجاوز 500 حرف',
            'payment_method.required' => 'اختر طريقة الدفع',
            'expense_date.required' => 'تاريخ المصروف مطلوب',
        ];
    }

    public function save(ExpenseService $service): void
    {
        $this->validate();

        try {
            $service->recordExpense([
                'branch_id' => $this->getActiveBranchId(),
                'category' => $this->category,
                'amount' => (int) round((float) $this->amount_display * 100),
                'description' => $this->description,
                'recipient_name' => $this->recipient_name ?: null,
                'payment_method' => $this->payment_method,
                'receipt_reference' => $this->receipt_reference ?: null,
                'expense_date' => $this->expense_date,
                'notes' => $this->notes ?: null,
            ], auth()->user());

            session()->flash('success', __('تم تسجيل المصروف بنجاح'));
            $this->redirect(route('expenses.list'), navigate: true);
        } catch (DomainException $e) {
            session()->flash('error', $e->getMessage());
        }
    }

    public function render()
    {
        return view('livewire.financial.expense-form', [
            'categories' => ExpenseCategory::cases(),
        ]);
    }
}
