<?php

namespace App\Livewire\Datatable\Master;

use App\Models\Category;
use App\Traits\Datatable\WithConfiguration;
use App\Traits\Datatable\WithPrependColumn;
use App\Traits\Media\WithMediaHandler;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Blade;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Columns\ImageColumn;

class CategoriesTable extends DataTableComponent
{
    use WithConfiguration, WithMediaHandler, WithPrependColumn;

    protected $model = Category::class;

    public function columns(): array
    {
        return [
            Column::make('Nama', 'name')->searchable(),

            Column::make('Jenis', 'type')
                ->label(fn ($row, $column) => Blade::render('<flux:badge color="'.$row->type->color().'">'.$row->type->label().'</flux:badge>'))
                ->html(),

            ImageColumn::make('Gambar')
                ->location(fn ($row) => optional($row->getMedia('image')->first())->getUrl() ?? asset('assets/images/logo.png'))
                ->attributes(fn ($row) => [
                    'style' => 'width: 50px; height: 50px;',
                    'alt' => $row->name,
                ]),

            Column::make('Aksi')
                ->label(function ($row) {
                    $actions = '';
                    $maxSortOrder = Category::max('sort_order');

                    if (auth()->user()->can('update category')) {
                        if ($row->sort_order > 1) {
                            $actions .= view('components.actions.table.sort-button', [
                                'id' => $row->hash,
                                'module' => 'category',
                                'direction' => 'up',
                                'tooltip' => 'Naik',
                                'color' => 'cyan',
                                'icon' => 'arrow-up',
                            ])->render();

                            $actions .= view('components.actions.table.sort-button', [
                                'id' => $row->hash,
                                'module' => 'category',
                                'direction' => 'first',
                                'tooltip' => 'Pertama',
                                'color' => 'emerald',
                                'icon' => 'bars-arrow-up',
                            ])->render();
                        }

                        if ($row->sort_order < $maxSortOrder) {
                            $actions .= view('components.actions.table.sort-button', [
                                'id' => $row->hash,
                                'module' => 'category',
                                'direction' => 'down',
                                'tooltip' => 'Turun',
                                'color' => 'orange',
                                'icon' => 'arrow-down',
                            ])->render();

                            $actions .= view('components.actions.table.sort-button', [
                                'id' => $row->hash,
                                'module' => 'category',
                                'direction' => 'last',
                                'tooltip' => 'Terakhir',
                                'color' => 'rose',
                                'icon' => 'bars-arrow-down',
                            ])->render();
                        }
                    }

                    if (auth()->user()->can('update category')) {
                        $actions .= view('components.actions.table.edit-modal', [
                            'id' => $row->hash,
                            'method' => 'update',
                            'modalTitle' => 'Ubah Kategori',
                        ])->render();
                    }

                    if (auth()->user()->can('delete category')) {
                        $actions .= view('components.actions.table.delete', [
                            'id' => $row->hash,
                        ])->render();
                    }

                    return $actions;
                })
                ->html()
                ->hideIf(auth()->user()->cannot('update category') && auth()->user()->cannot('delete category')),
        ];
    }

    public function builder(): Builder
    {
        return Category::select('id', 'name', 'type', 'sort_order')->orderBy('sort_order');
    }
}
