<?php

namespace App\Filament\Resources\Branches;

use Filament\Schemas\Schema;
use Filament\Actions\EditAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use App\Filament\Resources\Branches\Pages\ListBranches;
use App\Filament\Resources\Branches\Pages\CreateBranch;
use App\Filament\Resources\Branches\Pages\EditBranch;
use App\Filament\Resources\BranchResource\Pages;
use App\Filament\Resources\Branches\RelationManagers\BalancesRelationManager;
use App\Filament\Resources\Branches\RelationManagers\ExpenseRelationManager;
use App\Models\Branch;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Resource;
use Filament\Support\RawJs;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Validation\Rules\Unique;

class BranchResource extends Resource
{
    protected static ?string $model = Branch::class;

    protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-rectangle-stack';

    protected static bool $shouldRegisterNavigation = false;

    protected static ?string $recordTitleAttribute = 'code';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('client_id')->relationship('client', 'id')
                    ->getOptionLabelFromRecordUsing(fn ($record) => $record->company)
                    ->disabled()
                    ->columnSpan(2),

                TextInput::make('code')->required()
                    ->unique(
                        'branches',
                        'code',
                        ignoreRecord: true,
                        modifyRuleUsing: fn (Unique $rule) => $rule->where('client_id', app(static::getModel())->id)
                    ),

                TextInput::make('series')->label('Current Series')
                    ->required()
                    ->numeric()
                    ->integer()
                    ->maxLength(6)
                    ->minLength(1)
                    ->mask(RawJs::make(<<<'JS'
                         '999999'
                    JS)),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                //
            ])
            ->filters([
                //
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //            AccountsRelationManager::make(),
            BalancesRelationManager::make(),
            ExpenseRelationManager::make(),
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => ListBranches::route('/'),
            'create' => CreateBranch::route('/create'),
            'edit' => EditBranch::route('/{record}/edit'),
        ];
    }
}
