<?php

namespace App\Livewire\CashSessions;

use App\Domain\Financial\Models\CashSession;
use App\Domain\Financial\Services\CashSessionService;
use App\Domain\Identity\Models\Branch;
use App\Domain\Shared\Exceptions\DomainException;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Layout('layouts.app')]
#[Title('إدارة الوردية')]
class CashSessionManage extends Component
{
    public ?CashSession $currentSession = null;

    // Open form
    public ?int $branch_id = null;
    public float $opening_balance = 0;
    public string $opening_notes = '';

    // Close form
    public float $closing_balance = 0;
    public string $closing_notes = '';

    public function mount(): void
    {
        $this->currentSession = CashSession::where('user_id', auth()->id())
            ->where('status', 'open')
            ->first();

        if ($this->currentSession) {
            $expected = ($this->currentSession->opening_balance + $this->currentSession->total_cash_in - $this->currentSession->total_cash_out) / 100;
            $this->closing_balance = $expected;
        }
    }

    public function openSession(CashSessionService $service): void
    {
        $this->validate([
            'branch_id' => 'required|exists:branches,id',
            'opening_balance' => 'required|numeric|min:0',
        ], [
            'branch_id.required' => 'الفرع مطلوب',
            'branch_id.exists' => 'الفرع غير موجود',
            'opening_balance.required' => 'رصيد الافتتاح مطلوب',
            'opening_balance.min' => 'رصيد الافتتاح يجب ألا يكون سالب',
        ]);

        try {
            $amountInPiasters = (int) round($this->opening_balance * 100);
            $this->currentSession = $service->open(auth()->user(), $this->branch_id, $amountInPiasters, $this->opening_notes ?: null);
            $this->opening_balance = 0;
            $this->opening_notes = '';
            session()->flash('success', __('تم فتح الوردية بنجاح'));
        } catch (DomainException $e) {
            session()->flash('error', $e->getMessage());
        }
    }

    public function closeSession(CashSessionService $service): void
    {
        $this->validate([
            'closing_balance' => 'required|numeric|min:0',
        ], [
            'closing_balance.required' => 'رصيد الإغلاق مطلوب',
            'closing_balance.min' => 'رصيد الإغلاق يجب ألا يكون سالب',
        ]);

        try {
            $amountInPiasters = (int) round($this->closing_balance * 100);
            $service->close($this->currentSession, $amountInPiasters, $this->closing_notes ?: null);
            $this->currentSession = null;
            session()->flash('success', __('تم إغلاق الوردية بنجاح'));
        } catch (DomainException $e) {
            session()->flash('error', $e->getMessage());
        }
    }

    public function render()
    {
        return view('livewire.cash-sessions.cash-session-manage', [
            'branches' => Branch::orderBy('name_ar')->get(['id', 'name_ar']),
        ]);
    }
}
