<?php

namespace App\Livewire\Datatable\Studio\Catalog;

use App\Models\Brand;
use App\Traits\Datatable\WithConfiguration;
use App\Traits\Datatable\WithPrependColumn;
use App\Traits\WithMediaHandler;
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Columns\ImageColumn;

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

    protected $model = Brand::class;

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

            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 = Brand::max('sort_order');

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

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

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

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

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

                    if (auth()->user()->can('delete brand')) {
                        $actions .= view('components.datatables.delete', [
                            'id' => $row->hash,
                            'deleteRoute' => route('studio.catalog.brand.delete', $row->hash),
                        ])->render();
                    }

                    return $actions;
                })
                ->html(),
        ];
    }

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