<?php

namespace App\Filament\Resources\Company\UserCompanies\UserCompanies;

use Filament\Schemas\Schema;
use Filament\Actions\EditAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use App\Filament\Resources\Company\UserCompanies\Pages\ListUserCompanies;
use App\Filament\Resources\Company\UserCompanies\Pages\CreateUserCompany;
use App\Filament\Resources\Company\UserCompanies\Pages\EditUserCompany;
use App\Filament\Resources\Company\UserCompanyResource\Pages;
use App\Modules\Bank\Repositories\BankRepository;
use App\Modules\UserCompany\Models\UserCompany;
use App\Modules\UserCompany\Types\CompanyType;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;

class UserCompanyResource extends Resource
{
    protected static ?string $model = UserCompany::class;

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

    protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-building-office';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('company_type')
                    ->label('Kompaniýa görnüşi')
                    ->native(false)
                    ->options(CompanyType::options())
                    ->default(CompanyType::default())
                    ->required(),

                TextInput::make('name')
                    ->required(),

                TextInput::make('ssb')
                    ->label('Şahsy salgyt belgisi')
                    ->unique(ignoreRecord: true)
                    ->required(),

                TextInput::make('hb')
                    ->label('HB')
                    ->unique(ignoreRecord: true)
                    ->required(),

                Select::make('bank_id')
                    ->label('Bank')
                    ->native(false)
                    ->options(BankRepository::make()->options())
                    ->searchable()
                    ->required(),

                Toggle::make('default')
                    ->default(false)
                    ->label('Is default'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('company_type')
                    ->badge()
                    ->formatStateUsing(fn (string $state): string => CompanyType::option($state))
                    ->color(fn (string $state): string => CompanyType::statusClass($state)),

                TextColumn::make('ssb')
                    ->label('Şahsy salgyt belgisi')
                    ->searchable(),

                TextColumn::make('hb')
                    ->label('HB'),

                ToggleColumn::make('default'),
            ])
            ->filters([
                Filter::make('default'),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

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

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