<?php

namespace App\Filament\Admin\Resources;

use App\Enums\IconEnum;
use App\Models\User;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class UserResource extends Resource
{
    protected static ?int $navigationSort = 10;

    protected static ?string $model = User::class;

    protected static ?string $navigationIcon = IconEnum::USER->value;

    protected static ?string $navigationGroup = 'Settings';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make('Login details')
                    ->description(fn (string $operation) => $operation == 'edit' ? 'To leave the password unchanged, leave both fields empty,' : false)
                    ->schema([
                        TextInput::make('username')
                            ->autocomplete('new-username')
                            ->unique()
                            ->required()
                            ->columnSpanFull(),

                        TextInput::make('password')
                            ->password()
                            ->autocomplete('new-password')
                            ->revealable()
                            ->dehydrated(fn ($state) => ! empty($state))
                            ->required(fn (string $operation): bool => $operation === 'create'),

                        TextInput::make('password_verify')
                            ->label('Verify password')
                            ->password()
                            ->revealable()
                            ->same('password')
                            ->dehydrated(false)
                            ->required(fn (string $operation) => $operation === 'create'),
                    ]),

                Section::make('Permissions')
                    ->description('Administrators can access invoices and settings')
                    ->schema([
                        Toggle::make('is_admin')
                            ->columnSpanFull()
                            ->label('User is an administrator')
                            ->reactive()
                            ->afterStateUpdated(fn ($state, callable $set) => $set('customer_id', null))
                            ->disabled(fn (?User $record, $operation) => $operation !== 'create' && auth()->user()->id === $record->id),
                    ])
                    ->columns(2),

                Section::make('Customer Login')
                    ->description('If this account is for a customer, select them here')
                    ->schema([

                        Select::make('customer_id')
                            ->relationship('customer', 'company_name')
                            ->disabled(fn ($get) => $get('is_admin'))
                            ->afterStateUpdated(fn ($state, callable $set) => $set('is_admin', false))
                            ->nullable()
                            ->searchable()
                            ->preload(),
                    ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('username')
                    ->extraHeaderAttributes(['class' => 'w-full']),
                TextColumn::make('customer.company_name')
                    ->sortable()
                    ->placeholder('Internal'),
                Tables\Columns\IconColumn::make('is_admin')
                    ->label('Admin')
                    ->boolean()
                    ->alignRight(),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->modalWidth('md')
                    ->modal(),
            ])
            ->defaultSort('customer_id', 'asc');
    }

    public static function canAccess(): bool
    {
        return auth()->user()->is_admin ?? false;
    }

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

    public static function getPages(): array
    {
        return [
            'index' => \App\Filament\Admin\Resources\UserResource\Pages\ListUsers::route('/'),
        ];
    }

    private static function Grid() {}
}