<?php

namespace App\Livewire\Wallets;

use App\Domain\Financial\Models\Wallet;
use App\Domain\Financial\Models\WalletTransaction;
use App\Domain\Financial\Services\WalletService;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;
use Livewire\WithPagination;

#[Layout('layouts.app')]
#[Title('تفاصيل المحفظة')]
class WalletShow extends Component
{
    use WithPagination;

    public Wallet $wallet;

    public float $depositAmount = 0;
    public string $depositDescription = '';
    public float $withdrawAmount = 0;
    public string $withdrawDescription = '';

    public function mount(Wallet $wallet): void
    {
        // The route already carries `permission:wallets.view`; this is the
        // second of the five gates, and it is the one that still stands if the
        // component is ever mounted from anywhere but that route.
        //
        // The branch check is the model's: Wallet is branch-scoped, so route
        // binding resolves this id through BranchScope and another branch's
        // wallet 404s before deposit()/withdraw() can ever reach it.
        $this->authorize('wallets.view');

        $this->wallet = $wallet->load('owner');
    }

    public function deposit(WalletService $service): void
    {
        $this->validate([
            'depositAmount' => 'required|numeric|min:0.01',
            'depositDescription' => 'required|string|max:255',
        ], [
            'depositAmount.required' => 'المبلغ مطلوب',
            'depositAmount.min' => 'المبلغ يجب أن يكون أكبر من صفر',
            'depositDescription.required' => 'الوصف مطلوب',
            'depositDescription.max' => 'الوصف يجب ألا يتجاوز 255 حرف',
        ]);

        try {
            $amountInPiasters = (int) round($this->depositAmount * 100);
            $service->deposit($this->wallet, $amountInPiasters, $this->depositDescription, null, auth()->user());
            $this->wallet->refresh();
            $this->depositAmount = 0;
            $this->depositDescription = '';
            session()->flash('success', __('تم الإيداع بنجاح'));
        } catch (\Exception $e) {
            session()->flash('error', $e->getMessage());
        }
    }

    public function withdraw(WalletService $service): void
    {
        $this->validate([
            'withdrawAmount' => 'required|numeric|min:0.01',
            'withdrawDescription' => 'required|string|max:255',
        ], [
            'withdrawAmount.required' => 'المبلغ مطلوب',
            'withdrawAmount.min' => 'المبلغ يجب أن يكون أكبر من صفر',
            'withdrawDescription.required' => 'الوصف مطلوب',
            'withdrawDescription.max' => 'الوصف يجب ألا يتجاوز 255 حرف',
        ]);

        try {
            $amountInPiasters = (int) round($this->withdrawAmount * 100);
            $service->withdraw($this->wallet, $amountInPiasters, $this->withdrawDescription, null, auth()->user());
            $this->wallet->refresh();
            $this->withdrawAmount = 0;
            $this->withdrawDescription = '';
            session()->flash('success', __('تم السحب بنجاح'));
        } catch (\Exception $e) {
            session()->flash('error', $e->getMessage());
        }
    }

    public function render()
    {
        $transactions = WalletTransaction::where('wallet_id', $this->wallet->id)
            ->orderByDesc('created_at')
            ->paginate(20);

        return view('livewire.wallets.wallet-show', [
            'transactions' => $transactions,
        ]);
    }
}
