<?php

namespace App\Livewire\Studio\Master\Outlet;

use App\Enums\Day;
use App\Models\Outlet;
use App\Traits\WithCloseModal;
use App\Traits\WithConfirmation;
use App\Traits\WithMediaHandler;
use App\Traits\WithToast;
use Flux\Flux;
use Illuminate\Support\Str;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Title('Outlet')]
class Index extends Component
{
    use WithCloseModal, WithConfirmation, WithMediaHandler, WithToast;

    public array $outlets = [];

    public ?string $imageUrl = null;

    public string $search = '';

    public array $status = [];

    public function mount()
    {
        $this->loadOutlets();
    }

    protected function loadOutlets()
    {
        $this->outlets = Outlet::with(['openingHours', 'facilities'])
            ->when(! empty($this->search), fn ($query) => $query->where('name', 'like', '%'.$this->search.'%'))
            ->when(! empty($this->status), fn ($query) => $query->whereIn('status', $this->status))
            ->latest()
            ->get()
            ->map(fn ($outlet) => [
                'hash' => $outlet->hash,
                'name' => $outlet->name,
                'phone_number' => $outlet->phone_number,
                'landmark' => $outlet->landmark,
                'address' => $outlet->address,
                'status' => [
                    'label' => $outlet->status->label(),
                    'color' => $outlet->status->color(),
                ],
                'opening_hours' => $outlet->openingHours->mapWithKeys(fn ($hour) => [
                    Str::ucfirst(Str::lower(Day::from($hour->day->value)->label())) => [
                        'open' => formatTime($hour->open_time),
                        'close' => formatTime($hour->close_time),
                        'is_closed' => $hour->open_time === null && $hour->close_time === null,
                    ],
                ])->toArray(),
                'facilities' => $outlet->facilities->pluck('name')->toArray(),
                'opened_date' => formatDate($outlet->opened_date),
                'opened_ago' => timeAgo($outlet->opened_date),
                'closed_date' => formatDate($outlet->closed_date),
                'closed_ago' => timeAgo($outlet->closed_date),
                'image' => data_get($this->mapMediaCollection($outlet->getMedia('featured_image'))[0] ?? null, 'temporaryUrl', asset('assets/images/logo.png')),
                'images' => collect($this->mapMediaCollection($outlet->getMedia('images')))->pluck('temporaryUrl')->filter()->values(),
            ])
            ->toArray();
    }

    public function updatedSearch(string $value)
    {
        $this->search = $value;

        $this->loadOutlets();
    }

    public function updatedStatus()
    {
        $this->loadOutlets();
    }

    public function openImage(string $imageUrl)
    {
        $this->imageUrl = $imageUrl;

        Flux::modal('image-modal')->show();
    }

    public function delete(Outlet $outlet)
    {
        $outlet->delete();

        $this->toast('Outlet berhasil dihapus.');

        Flux::modals()->close();
    }

    public function render()
    {
        return view('livewire.studio.master.outlet.index', [
            'pageTitle' => 'Outlet',
        ]);
    }
}
