<?php

namespace App\Filament\Resources;

use App\Filament\Resources\UserResource\Pages;
use App\Filament\Resources\UserResource\RelationManagers;
use App\Models\User;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Hash;
use pxlrbt\FilamentExcel\Actions\Tables\ExportAction;
use pxlrbt\FilamentExcel\Exports\ExcelExport;

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

    protected static ?string $navigationIcon = 'heroicon-o-user-group';
    protected static ?string $navigationGroup = 'مدیریت سیستم';
    protected static ?string $navigationLabel = 'کاربران و کارمندان';
    protected static ?string $modelLabel = 'کاربر';
    protected static ?string $pluralModelLabel = 'کاربران';
    protected static ?int $navigationSort = 1;

    public static function form(Form $form): Form
    {
        return $form->schema([
            Forms\Components\TextInput::make('name')->label('نام')->required(),
            Forms\Components\TextInput::make('email')->label('ایمیل')->email()->required()->unique(ignoreRecord: true),
            Forms\Components\TextInput::make('phone')->label('تلفن'),
            Forms\Components\TextInput::make('job_title')->label('عنوان شغلی')->placeholder('مدیر فروش / کارشناس فروش'),
            Forms\Components\TextInput::make('password')->label('رمز عبور')
                ->password()->revealable()
                ->dehydrateStateUsing(fn ($state) => Hash::make($state))
                ->dehydrated(fn ($state) => filled($state))
                ->required(fn (string $operation) => $operation === 'create'),
            Forms\Components\Select::make('roles')->label('نقش')
                ->relationship('roles', 'name')
                ->getOptionLabelFromRecordUsing(fn ($record) => \App\Support\AccessControl::roleLabel($record->name))
                ->multiple()->preload()->required(),
            Forms\Components\TextInput::make('bonus_percent')->label('درصد پاداش فروش')
                ->numeric()->default(0)->suffix('٪')
                ->helperText('درصدی از مجموع فروش که به‌عنوان پاداش محاسبه می‌شود'),
            Forms\Components\Toggle::make('is_active')->label('فعال')->default(true),
        ])->columns(2);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')->label('نام')->searchable()->sortable(),
                Tables\Columns\TextColumn::make('email')->label('ایمیل')->searchable(),
                Tables\Columns\TextColumn::make('roles.name')->label('نقش')->badge()
                    ->formatStateUsing(fn ($state) => \App\Support\AccessControl::roleLabel($state)),
                Tables\Columns\TextColumn::make('job_title')->label('عنوان شغلی')->toggleable(),
                Tables\Columns\TextColumn::make('bonus_percent')->label('درصد پاداش')->suffix('٪'),
                Tables\Columns\IconColumn::make('is_active')->label('فعال')->boolean(),
                Tables\Columns\TextColumn::make('created_at')->label('تاریخ ثبت')->jalaliDate()->toggleable(isToggledHiddenByDefault: true),
            ])
            ->actions([Tables\Actions\EditAction::make()])
            ->headerActions([
                ExportAction::make()->label('خروجی اکسل')->exports([ExcelExport::make()->fromTable()]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            RelationManagers\ActivitiesRelationManager::class,
        ];
    }

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