<?php

namespace App\Filament\Resources\Clients;

use Filament\Pages\Enums\SubNavigationPosition;
use Filament\Schemas\Schema;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Grid;
use Filament\Forms\Components\Select;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Actions\ViewAction;
use Filament\Actions\EditAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use App\DataObjects\CreateBranchDTO;
use App\Filament\Resources\Clients\Pages\EditClient;
use App\Filament\Resources\Clients\Pages\ListClients;
use App\Filament\Resources\Clients\Pages\ViewClient;
use App\Filament\Resources\Clients\Pages\GeneralLedger;
use App\Filament\Resources\Clients\Pages\TrialBalance;
use App\Filament\Resources\Clients\RelationManagers\AccountsRelationManager;
use App\Filament\Resources\Clients\RelationManagers\BranchesRelationManager;
use App\Filament\Resources\Clients\RelationManagers\DiscountRelationManager;
use App\Filament\Resources\Clients\RelationManagers\ExpensesRelationManager;
use App\Filament\Resources\Clients\RelationManagers\JournalsRelationManager;
use App\Filament\Resources\Clients\RelationManagers\SalesRelationManager;
use App\Filament\Resources\Clients\RelationManagers\TransmittalsRelationManager;
use App\Models\Branch;
use App\Models\Client;
use App\Processes\Branch\CreateBranchProcess;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Filament\Resources\Pages\Page;

class ClientResource extends Resource
{
    protected static ?string $model = Client::class;

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

    protected static ?string $recordTitleAttribute = 'company';
    
    protected static ?\Filament\Pages\Enums\SubNavigationPosition $subNavigationPosition = SubNavigationPosition::Top;

    public static function authorizeView(Model $record): void
    {
        parent::authorizeView($record);
    }

    public static function getRecordSubNavigation(Page $page): array
    {
        return $page->generateNavigationItems([
            ViewClient::class,
            EditClient::class,
            GeneralLedger::class,
            TrialBalance::class,
        ]);
    }

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('firstname')->label('First Name')->required(),
                TextInput::make('middlename')->label('Middle Name')->nullable(),
                TextInput::make('lastname')->label('Last Name')->required(),
                Grid::make()->schema([
                    TextInput::make('company')->label('Company')->required(),
                    Select::make('type_id')
                        ->relationship('type', 'type')
                        ->label('Type')->required(),
                ])->columns(2),
            ])->columns(3);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('firstname')->label('First Name')->searchable(),
                TextColumn::make('middlename')->label('Middle Name'),
                TextColumn::make('lastname')->label('Last Name'),
                TextColumn::make('company')->label('Company')->searchable(),
                TextColumn::make('type.type')->label('Type'),
            ])
            ->filters([
                Filter::make('Vatable')
                    ->query(fn (Builder $query) => $query->orWhereHas('type', function (Builder $query) {
                        $query->where('type', 'Vatable');
                    })),
                Filter::make('Non-Vatable')
                    ->query(fn (Builder $query) => $query->orWhereHas('type', function (Builder $query) {
                        $query->where('type', 'Non Vatable');
                    })),
            ])
            ->recordActions([
                ViewAction::make(),
                EditAction::make(),
                DeleteAction::make()->requiresConfirmation(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            AccountsRelationManager::class,
            BranchesRelationManager::class,
            TransmittalsRelationManager::class,
            SalesRelationManager::class,
            ExpensesRelationManager::class,
            JournalsRelationManager::class,
            DiscountRelationManager::class,
        ];
    }

    public static function getPages(): array
    {
        return [
            'view' => ViewClient::route('/{record}'),
            'edit' => EditClient::route('/{record}/edit'),
            'index' => ListClients::route('/'),
            'general-ledger' => GeneralLedger::route('/{record}/general-ledger'),
            'trial-balance' => TrialBalance::route('/{record}/trial-balance'),
        ];
    }

    public static function saveBranch($data): Branch
    {
        $createBranchProcess = new CreateBranchProcess;
        $payload = new CreateBranchDTO(data: $data);

        return $createBranchProcess->run($payload)->branch;
    }
}
