<?php

namespace App\Http\Livewire;

use App\Models\TransactionType;
use Livewire\Component;

class TransactionTypeRadio extends Component
{
    public $type;
    public $typeOptions;
    public $transactionTypeSelected;

    protected $listeners = [
        'transactionTypeOptions' => 'transactionTypeOptionsDispatched'
    ];


    public function mount($type = null)
    {
        if ($type) {
            $typeSelected = ($type == 'worked time' || $type == 1) ? 'worked time' : $this->transactionTypeSelected;
            $typeSelected = ($type == 'gift' || $type == 2) ? 'gift' : $this->transactionTypeSelected;
            $typeSelected = ($type == 'donation' || $type == 3) ? 'donation' : $this->transactionTypeSelected;

            if ($typeSelected) {
                $this->transactionTypeSelected = $typeSelected;
            }
        }

        if ($this->transactionTypeSelected) {
            $this->updated();
        }
    }


    public function updated($propertyName = null)
    {
        if ($this->transactionTypeSelected === null || $this->transactionTypeSelected === '') {
            // Explicitly dispatch null when no selection
            $this->dispatch('transactionTypeSelected', null);
        } else {
            $selected = TransactionType::where('name', $this->transactionTypeSelected)->first();
            $this->dispatch('transactionTypeSelected', $selected);
        }
    }


    public function transactionTypeOptionsDispatched($typeOptions, $rememberedType = null)
    {
        if ($typeOptions == null) {
            $this->reset('typeOptions');
            $this->reset('transactionTypeSelected');
        } else {
            $this->typeOptions = TransactionType::find($typeOptions);

            if ($rememberedType) {
                $rememberedTypeAvailable = $this->typeOptions->firstWhere('id', $rememberedType['id']);

                if ($rememberedTypeAvailable) {
                    // Remembered type is available - select it
                    $this->transactionTypeSelected = $rememberedTypeAvailable->name;
                    $this->updated();
                } else {
                    // Remembered type not available - check if it's gift or donation
                    $rememberedTypeName = $rememberedType['name'] ?? '';
                    $rememberedTypeNameLower = strtolower($rememberedTypeName);

                    if ($rememberedTypeNameLower === 'gift' || $rememberedTypeNameLower === 'donation') {
                        // Gift or donation not available - don't auto-select anything
                        $this->transactionTypeSelected = '';
                        $this->dispatch('transactionTypeSelected', null);
                        // Don't call updated() to avoid any auto-selection
                    } else {
                        // Other type not available - use default selection
                        $this->setDefaultSelection();
                        $this->updated();
                    }
                }
            } else {
                // No remembered type - use default selection
                $this->setDefaultSelection();
                $this->updated();
            }
        }
    }

    /**
     * Set default transaction type selection (worked time or first option)
     */
    private function setDefaultSelection()
    {
        // Check if 'worked time' exists in the options and pre-select this option
        $workedTimeOption = $this->typeOptions->firstWhere('name', 'worked time');
        if ($workedTimeOption) {
            $this->transactionTypeSelected = 'worked time';
        } else {
            // Optionally set to the first available option
            $this->transactionTypeSelected = $this->typeOptions->first()->name ?? null;
        }
    }

    public function render()
    {
        return view('livewire.transaction-type-radio');
    }
}
