<?php

declare(strict_types=1);

namespace App\Livewire\Admin\PaymentMethods;

use App\Models\PaymentMethod;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Layout;
use Livewire\Component;

#[Layout('layouts.admin')]
final class Index extends Component
{
    public string $search = '';

    public string $statusFilter = '';

    public string $typeFilter = '';

    public string $gatewayTypeFilter = '';

    public function updatingSearch(): void
    {
        $this->resetPage();
    }

    public function updatingStatusFilter(): void
    {
        $this->resetPage();
    }

    public function updatingTypeFilter(): void
    {
        $this->resetPage();
    }

    public function updatingGatewayTypeFilter(): void
    {
        $this->resetPage();
    }

    #[Computed]
    public function paymentMethods()
    {
        return PaymentMethod::query()
            ->when($this->search, fn ($q) => $q->where('name', 'like', "%{$this->search}%")
                ->orWhere('type', 'like', "%{$this->search}%"))
            ->when($this->statusFilter, fn ($q) => $q->where('is_active', $this->statusFilter === 'active'))
            ->when($this->typeFilter, fn ($q) => $q->where('type', $this->typeFilter))
            ->when($this->gatewayTypeFilter, fn ($q) => $q->where('gateway_type', $this->gatewayTypeFilter))
            ->orderBy('display_order')
            ->paginate(15);
    }

    #[Computed]
    public function stats()
    {
        return [
            'total' => PaymentMethod::count(),
            'active' => PaymentMethod::where('is_active', true)->count(),
            'inactive' => PaymentMethod::where('is_active', false)->count(),
            'automated' => PaymentMethod::where('gateway_type', 'automated')->count(),
            'manual' => PaymentMethod::where('gateway_type', 'manual')->count(),
        ];
    }

    public function toggleStatus(PaymentMethod $paymentMethod): void
    {
        $paymentMethod->update(['is_active' => ! $paymentMethod->is_active]);

        activity()
            ->causedBy(auth()->user())
            ->performedOn($paymentMethod)
            ->withProperties([
                'payment_method_id' => $paymentMethod->id,
                'new_status' => $paymentMethod->is_active ? 'active' : 'inactive',
            ])
            ->log('payment_method_status_toggled');

        session()->flash('success', "Payment method {$paymentMethod->name} ".($paymentMethod->is_active ? 'activated' : 'deactivated').' successfully!');
    }

    public function deletePaymentMethod(PaymentMethod $paymentMethod): void
    {
        activity()
            ->causedBy(auth()->user())
            ->performedOn($paymentMethod)
            ->withProperties([
                'payment_method_id' => $paymentMethod->id,
                'name' => $paymentMethod->name,
                'type' => $paymentMethod->type,
            ])
            ->log('payment_method_deleted');

        $paymentMethod->delete();

        session()->flash('success', 'Payment method deleted successfully!');
    }

    public function render()
    {
        return view('livewire.admin.payment-methods.index');
    }
}
