<?php

namespace App\Filament\Resources;

use App\Filament\Resources\StoreGroupResource\Pages;
use App\Models\StoreGroup;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class StoreGroupResource extends Resource
{
    protected static ?string $model = StoreGroup::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function getModelLabel(): string
    {
        return 'Agrupador';
    }

    public static function getPluralModelLabel(): string
    {
        return 'Agrupadores';
    }

    public static function getNavigationLabel(): string
    {
        return 'Agrupadores';
    }

    public static function getModuleDescription(): string
    {
        return match (app()->getLocale()) {
            'en' => 'Define store groups used to segment operations and reporting.',
            default => 'Define agrupadores de tiendas para segmentar operaciones y reportes.',
        };
    }

    public static function canViewAny(): bool
    {
        return auth()->user()?->can('store_groups.view') ?? false;
    }

    public static function canCreate(): bool
    {
        return auth()->user()?->can('store_groups.create') ?? false;
    }

    public static function canEdit($record): bool
    {
        return auth()->user()?->can('store_groups.update') ?? false;
    }

    public static function canDelete($record): bool
    {
        return auth()->user()?->can('store_groups.delete') ?? false;
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->label('Nombre')
                    ->required()
                    ->maxLength(255)
                    ->unique(ignoreRecord: true),
                Forms\Components\Toggle::make('is_active')
                    ->label('Activo')
                    ->default(true),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')
                    ->label('Nombre')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('stores_count')
                    ->label('Tiendas')
                    ->counts('stores')
                    ->sortable(),
                Tables\Columns\ToggleColumn::make('is_active')
                    ->label('Activo')
                    ->disabled(fn () => ! (auth()->user()?->can('store_groups.update') ?? false))
                    ->sortable(),
            ])
            ->filters([
                Tables\Filters\TernaryFilter::make('is_active')
                    ->label('Activo'),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => auth()->user()?->can('store_groups.delete') ?? false),
            ])
            ->bulkActions([]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListStoreGroups::route('/'),
            'create' => Pages\CreateStoreGroup::route('/create'),
            'edit' => Pages\EditStoreGroup::route('/{record}/edit'),
        ];
    }
}
