<?php

namespace App\Http\Livewire;

use Carbon\Carbon;
use Livewire\Component;

class SelectPeriod extends Component
{
    public $selectedPeriod = 'previous_year';
    public $customFromDate;
    public $customToDate;
    public $label;

    protected $rules = [
        'customFromDate' => 'nullable|date',
        'customToDate' => 'nullable|date|after_or_equal:customFromDate',
    ];

    protected $messages = [
        'customFromDate.date' => 'The from date must be a valid date.',
        'customToDate.date' => 'The to date must be a valid date.',
        'customToDate.after_or_equal' => 'The to date must be after or equal to the from date.',
    ];

    public function mount($label = 'Select Period')
    {
        $this->label = $label;
        $this->setPeriodDates();
    }

    public function updatedSelectedPeriod()
    {
        $this->setPeriodDates();
    }

    public function applyCustomPeriod(string $fromDate = '', string $toDate = '')
    {
        $this->customFromDate = $fromDate ?: null;
        $this->customToDate   = $toDate ?: null;
        $this->validate();
        $this->dispatchDates();
    }

    private function setPeriodDates()
    {
        $fromDate = null;
        $toDate = null;

        switch ($this->selectedPeriod) {
            case 'previous_year':
                $fromDate = Carbon::now()->subYear()->startOfYear()->format('Y-m-d');
                $toDate = Carbon::now()->subYear()->endOfYear()->format('Y-m-d');
                break;

            case 'previous_quarter':
                $fromDate = Carbon::now()->subQuarter()->startOfQuarter()->format('Y-m-d');
                $toDate = Carbon::now()->subQuarter()->endOfQuarter()->format('Y-m-d');
                break;

            case 'previous_month':
                $fromDate = Carbon::now()->subMonth()->startOfMonth()->format('Y-m-d');
                $toDate = Carbon::now()->subMonth()->endOfMonth()->format('Y-m-d');
                break;

            case 'custom':
                $fromDate = $this->customFromDate;
                $toDate = $this->customToDate;
                break;
        }

        if ($this->selectedPeriod !== 'custom') {
            $this->customFromDate = $fromDate;
            $this->customToDate = $toDate;
        }

        $this->dispatchDates();
    }

    private function dispatchDates()
    {
        // If no period is selected, dispatch clear event
        if (!$this->selectedPeriod) {
            $this->dispatch('periodCleared');
            return;
        }

        // For custom periods, ensure both dates are provided before dispatching
        if ($this->selectedPeriod === 'custom') {
            if (!$this->customFromDate || !$this->customToDate) {
                // If custom is selected but dates are missing, dispatch clear
                $this->dispatch('periodCleared');
                return;
            }
        }

        $this->dispatch('periodSelected', [
            'fromDate' => $this->customFromDate,
            'toDate' => $this->customToDate,
            'period' => $this->selectedPeriod
        ]);
    }

    public function render()
    {
        return view('livewire.select-period');
    }
}