<?php

namespace App\Livewire\Projects;

use App\Models\Project;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\On;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;

class ProjectUsersTable extends DataTableComponent
{
    protected $model = User::class;

    public int $projectId;

    /** role_in_project => label */
    public const ROLES = [
        'supervisor' => 'Supervisor',
        'consultant' => 'Consultor',
        'client' => 'Cliente',
        'viewer' => 'Observador',
    ];

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

    #[On('project-users-changed')]
    public function refreshRows(): void
    {
        // no-op: triggers re-render so the builder re-runs.
    }

    public function builder(): Builder
    {
        return User::query()
            ->join('project_user', 'project_user.user_id', '=', 'users.id')
            ->where('project_user.project_id', $this->projectId);
    }

    public function columns(): array
    {
        return [
            Column::make('Nombre', 'name')
                ->sortable()
                ->searchable()
                ->format(function ($value, $row) {
                    $initial = strtoupper(mb_substr($value ?? '?', 0, 1));

                    return '<div class="flex items-center gap-2">
                          <span class="w-7 h-7 rounded-full bg-primary text-primary-content flex items-center justify-center text-xs font-bold shrink-0">'.$initial.'</span>
                          <span class="font-medium">'.e($value).'</span>
                      </div>';
                })
                ->html(),

            Column::make('Email', 'email')
                ->sortable()
                ->searchable(),

            Column::make('Rol', 'role_in_project')
                ->label(function ($row) {
                    $current = $row->role_in_project;
                    if (! Auth::user()->can('assign users')) {
                        return '<span class="badge badge-sm">'.(self::ROLES[$current] ?? ucfirst((string) $current)).'</span>';
                    }
                    $opts = '';
                    foreach (self::ROLES as $val => $label) {
                        $opts .= '<option value="'.$val.'"'.($current === $val ? ' selected' : '').'>'.$label.'</option>';
                    }

                    return '<select wire:change="changeRole('.$row->id.', $event.target.value)" class="select select-bordered select-xs">'.$opts.'</select>';
                })
                ->html(),

            Column::make('Acciones')
                ->label(function ($row) {
                    if (! Auth::user()->can('assign users')) {
                        return '';
                    }

                    return '<div class="flex justify-end">
                          <button wire:click="removeUser('.$row->id.')" wire:confirm="¿Quitar a '.e($row->name).' del proyecto?"
                              class="btn btn-xs btn-error btn-outline" title="Quitar del proyecto">
                              <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="M6 18L18 6M6 6l12 12"/></svg>
                          </button>
                      </div>';
                })
                ->html(),
        ];
    }

    public function filters(): array
    {
        return [
            SelectFilter::make('Rol', 'role')
                ->options(['' => 'Rol: todos'] + self::ROLES)
                ->filter(fn (Builder $query, string $value) => $query->where('project_user.role_in_project', $value)),
        ];
    }

    public function changeRole($userId, $role): void
    {
        abort_unless(Auth::user()->can('assign users'), 403);
        if (! array_key_exists($role, self::ROLES)) {
            return;
        }
        Project::findOrFail($this->projectId)
            ->users()->updateExistingPivot($userId, ['role_in_project' => $role]);
        $this->dispatch('project-users-changed');
        $this->dispatch('notify', 'Rol actualizado.');
    }

    public function removeUser($userId): void
    {
        abort_unless(Auth::user()->can('assign users'), 403);
        Project::findOrFail($this->projectId)->users()->detach($userId);
        $this->dispatch('project-users-changed');
        $this->dispatch('notify', 'Usuario eliminado del proyecto.');
    }
}
