<?php

namespace App\Filament\Tenant\Resources;

use App\Filament\Tenant\Resources\UserResource\Pages;
use App\Models\Tenant\User;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Actions;
use Filament\Schemas;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Hash;

class UserResource extends Resource
{
    protected static ?string $model = User::class;

    protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-user-group';

    protected static ?string $navigationLabel = 'Utilizatori';

    protected static string|\UnitEnum|null $navigationGroup = 'Admin';

    protected static ?string $modelLabel = 'utilizator';

    protected static ?string $pluralModelLabel = 'utilizatori';

    protected static ?int $navigationSort = 80;

    public static function canViewAny(): bool
    {
        $u = auth()->user();
        return $u && $u->role === 'admin';
    }

    public static function form(Schema $schema): Schema
    {
        return $schema->components([
            Schemas\Components\Section::make('Identitate')
                ->columns(2)
                ->schema([
                    Forms\Components\TextInput::make('name')->label('Nume')->required()->maxLength(120),
                    Forms\Components\TextInput::make('email')->email()->required()->maxLength(120),
                    Forms\Components\TextInput::make('phone')->tel()->maxLength(40),
                    Forms\Components\Select::make('locale')
                        ->options(['ro' => 'Română', 'ru' => 'Русский', 'en' => 'English'])
                        ->default('ro'),
                ]),
            Schemas\Components\Section::make('Acces')
                ->columns(2)
                ->schema([
                    Forms\Components\Select::make('role')
                        ->label('Rol primar')
                        ->options([
                            'admin' => 'Administrator',
                            'manager' => 'Manager',
                            'receptionist' => 'Recepție',
                            'mechanic' => 'Mecanic',
                            'parts_manager' => 'Magazioner piese',
                            'accountant' => 'Contabil',
                            'marketer' => 'Marketing',
                        ])
                        ->required()
                        ->default('mechanic'),
                    Forms\Components\Select::make('status')
                        ->options(['active' => 'Activ', 'inactive' => 'Inactiv', 'blocked' => 'Blocat'])
                        ->default('active')
                        ->required(),
                    Forms\Components\TextInput::make('password')
                        ->label('Parolă')
                        ->password()
                        ->required(fn (string $context) => $context === 'create')
                        ->dehydrated(fn ($state) => filled($state))
                        ->dehydrateStateUsing(fn ($state) => Hash::make($state))
                        ->minLength(6)
                        ->helperText('La editare lasă gol pentru a păstra parola actuală.'),
                ]),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
                Tables\Columns\TextColumn::make('email')->searchable()->copyable(),
                Tables\Columns\TextColumn::make('phone')->placeholder('—'),
                Tables\Columns\TextColumn::make('role')->badge(),
                Tables\Columns\TextColumn::make('status')
                    ->badge()
                    ->colors([
                        'success' => ['active'],
                        'warning' => ['inactive'],
                        'danger' => ['blocked'],
                    ]),
                Tables\Columns\TextColumn::make('last_login_at')->dateTime()->placeholder('—')->toggleable(),
                Tables\Columns\TextColumn::make('created_at')->date()->sortable()->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('role')->options([
                    'admin' => 'Admin', 'manager' => 'Manager', 'receptionist' => 'Recepție',
                    'mechanic' => 'Mecanic', 'parts_manager' => 'Magazie', 'accountant' => 'Contabil', 'marketer' => 'Marketing',
                ]),
                Tables\Filters\SelectFilter::make('status')->options([
                    'active' => 'Activ', 'inactive' => 'Inactiv', 'blocked' => 'Blocat',
                ]),
            ])
            ->actions([
                Actions\EditAction::make(),
                Actions\DeleteAction::make(),
            ])
            ->defaultSort('created_at', 'desc');
    }

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