<?php

namespace App\Livewire\Projects;

use App\Models\Feature;
use App\Models\Layer;
use App\Models\Phase;
use App\Models\Project;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
use Rappasoft\LaravelLivewireTables\Views\Filters\TextFilter;

class FeatureTable extends DataTableComponent
{
    protected $model = Feature::class;

    public int $projectId;

    public function configure(): void
    {
        $this->setPrimaryKey('id')
            ->setDefaultSort('name', 'asc')
            ->setSortingPillsEnabled(false)
            ->setSecondaryHeaderEnabled()
            ->setAdditionalSelects(['features.id as id', 'features.layer_id as layer_id']);
    }

    public function builder(): Builder
    {
        $user = Auth::user();
        abort_unless(
            $user->can('manage all') ||
            Project::whereKey($this->projectId)->whereHas('users', fn ($q) => $q->where('user_id', $user->id))->exists(),
            403
        );

        return Feature::query()
            ->whereHas('layer.phase', fn ($q) => $q->where('project_id', $this->projectId))
            ->with(['layer.phase']);
    }

    public function columns(): array
    {
        return [
            Column::make('Elemento', 'name')
                ->sortable()
                ->searchable()
                ->secondaryHeaderFilter('name')
                ->format(fn ($value) => '<span class="font-medium">'.e($value).'</span>')
                ->html(),

            Column::make('Capa')
                ->secondaryHeaderFilter('layer')
                ->label(fn ($row) => e($row->layer?->name ?? '—')),

            Column::make('Fase')
                ->secondaryHeaderFilter('phase')
                ->label(fn ($row) => e($row->layer?->phase?->name ?? '—')),

            Column::make('Progreso', 'progress')
                ->sortable()
                ->format(function ($value) {
                    $cls = $value >= 100 ? 'badge-success' : ($value > 0 ? 'badge-warning' : 'badge-ghost');

                    return '<span class="badge badge-sm '.$cls.'">'.(int) $value.'%</span>';
                })
                ->html(),

            Column::make('Acciones')
                ->label(function ($row) {
                    $id = $row->id;

                    return '<div class="flex justify-end">'
                        .'<button wire:click="$dispatch(\'map-select-feature\', { featureId: '.$id.' })"'
                        .'class="btn btn-xs btn-primary gap-1" title="'.e(__('Editar elemento')).'">'
                        .'<svg xmlns="http://www.w3.org/2000/svg" class="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/></svg>'
                        .e(__('Abrir'))
                        .'</button>'
                        .'</div>';
                })
                ->html(),
        ];
    }

    public function filters(): array
    {
        $layers = Layer::whereHas('phase', fn ($q) => $q->where('project_id', $this->projectId))
            ->orderBy('name')->pluck('name', 'id')->toArray();
        $phases = Phase::where('project_id', $this->projectId)
            ->orderBy('order')->pluck('name', 'id')->toArray();

        return [
            TextFilter::make('Elemento', 'name')
                ->config(['placeholder' => 'Buscar elemento…'])
                ->filter(fn (Builder $query, string $value) => $query->where('features.name', 'like', '%'.$value.'%')),

            SelectFilter::make('Capa', 'layer')
                ->options(['' => 'Todas'] + $layers)
                ->filter(fn (Builder $query, string $value) => $query->where('features.layer_id', $value)),

            SelectFilter::make('Fase', 'phase')
                ->options(['' => 'Todas'] + $phases)
                ->filter(fn (Builder $query, string $value) => $query->whereHas('layer', fn ($l) => $l->where('phase_id', $value))),
        ];
    }
}
