<?php

namespace Daylight\Core\Livewire\Store;

use Daylight\Core\Livewire\Tables\BaseTable;
use Daylight\Core\Livewire\Tables\Concerns\WithBulkActions;
use Daylight\Core\Livewire\Tables\Concerns\WithFiltering;
use Daylight\Core\Livewire\Tables\Concerns\WithRowActions;
use Daylight\Core\Livewire\Tables\Concerns\WithSearch;
use Daylight\Core\Livewire\Tables\Concerns\WithSorting;
use Daylight\Core\Models\Enums\PublishStatus;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\View\View;
use Livewire\Attributes\Computed;

class LabelTable extends BaseTable
{
    use WithBulkActions;
    use WithFiltering;
    use WithRowActions;
    use WithSearch;
    use WithSorting;

    protected function getBaseQuery(): Builder
    {
        return (daylightModel('label'))::query();
    }

    protected function applySearch(Builder $query, ?string $searchQuery): Builder
    {
        return $query->when($searchQuery, function ($query, $searchQuery) {
            $query->whereHas('translations', function ($query) use ($searchQuery) {
                $query->where('name', 'like', "%$searchQuery%");
            });
        });
    }

    protected function sortableColumns(): array
    {
        return ['name', 'created_at'];
    }

    protected function applySorting(Builder $query): Builder
    {
        if ($this->sortField === 'name') {
            return $query->orderBy(
                (daylightModel('label_translation'))::select('name')
                    ->whereColumn('label_id', 'labels.id')
                    ->where('locale', app()->getLocale())
                    ->limit(1),
                $this->sortDirection
            );
        }

        return $query->orderBy($this->sortField, $this->sortDirection);
    }

    protected function applyFilter(Builder $query, string $name, mixed $value): Builder
    {
        return match ($name) {
            'status' => $query->where('status', $value),
            default => $query,
        };
    }

    protected function availableFilters(): array
    {
        return [
            'status' => [
                'label' => __('daylight::general.all_statuses'),
                'options' => PublishStatus::formattedValues()->toArray(),
            ],
        ];
    }

    #[Computed]
    protected function availableBulkActions(): array
    {
        return [
            'bulk_delete' => [
                'name' => __('daylight::general.delete'),
                'action' => 'bulkDelete',
                'confirm' => true,
            ],
            'bulk_publish' => [
                'name' => __('daylight::general.published'),
                'action' => 'bulkPublish',
            ],
            'bulk_draft' => [
                'name' => __('daylight::general.draft'),
                'action' => 'bulkDraft',
            ],
        ];
    }

    public function bulkPublish(): void
    {
        $this->bulkStatusChange(PublishStatus::PUBLISHED->value);
    }

    public function bulkDraft(): void
    {
        $this->bulkStatusChange(PublishStatus::DRAFT->value);
    }

    public function render(): View
    {
        return view('daylight::livewire.store.label-table');
    }
}
