<?php

namespace App\Filament\Resources\Projects\Tables;

use App\Enums\ProjectStatus;
use App\Models\Project;
use Filament\Actions\Action;
use Filament\Actions\ViewAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class ProjectsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')->label(__('admin.project'))->searchable()->sortable(),
                TextColumn::make('domain')->label(__('admin.domain'))->searchable()->sortable(),
                TextColumn::make('user.name')->label(__('admin.customer'))->searchable()->sortable(),
                TextColumn::make('user.email')->label(__('admin.email'))->searchable(),
                TextColumn::make('status')->label(__('admin.status'))->badge()->formatStateUsing(fn ($state) => $state->label()),
                TextColumn::make('created_at')->label(__('admin.created_at'))->dateTime('d.m.Y H:i')->sortable(),
                TextColumn::make('updated_at')->label(__('admin.updated_at'))->dateTime('d.m.Y H:i')->sortable(),
            ])
            ->filters([SelectFilter::make('status')->label(__('admin.status'))->options(['active' => __('states.active'), 'archived' => __('states.archived')])])
            ->recordActions([
                ViewAction::make(),
                Action::make('archive')->label(__('admin.archive'))->requiresConfirmation()
                    ->visible(fn (Project $record): bool => $record->status === ProjectStatus::Active && auth()->user()->can('archive', $record))
                    ->action(fn (Project $record) => $record->archive()),
                Action::make('restore')->label(__('admin.restore'))
                    ->visible(fn (Project $record): bool => $record->status === ProjectStatus::Archived && auth()->user()->can('restore', $record))
                    ->action(fn (Project $record) => $record->restoreProject()),
            ])
            ->toolbarActions([]);
    }
}
