<?php

namespace App\Filament\Resources;

use App\DataObjects\CreateBranchDTO;
use App\Filament\Resources\ClientResource\Pages\EditClient;
use App\Filament\Resources\ClientResource\Pages\ListClients;
use App\Filament\Resources\ClientResource\Pages\ViewClient;
use App\Filament\Resources\ClientResource\Pages\GeneralLedger;
use App\Filament\Resources\ClientResource\Pages\TrialBalance;
use App\Filament\Resources\ClientResource\RelationManagers\AccountsRelationManager;
use App\Filament\Resources\ClientResource\RelationManagers\BranchesRelationManager;
use App\Filament\Resources\ClientResource\RelationManagers\ExpensesRelationManager;
use App\Filament\Resources\ClientResource\RelationManagers\JournalsRelationManager;
use App\Filament\Resources\ClientResource\RelationManagers\SalesRelationManager;
use App\Filament\Resources\ClientResource\RelationManagers\TransmittalsRelationManager;
use App\Models\Branch;
use App\Models\Client;
use App\Processes\Branch\CreateBranchProcess;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Filament\Pages\SubNavigationPosition;
use Filament\Resources\Pages\Page;

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

    protected static ?string $navigationIcon = 'heroicon-o-user';

    protected static ?string $recordTitleAttribute = 'company';
    
    protected static 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(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('firstname')->label('First Name')->required(),
                Forms\Components\TextInput::make('middlename')->label('Middle Name')->nullable(),
                Forms\Components\TextInput::make('lastname')->label('Last Name')->required(),
                Forms\Components\Grid::make()->schema([
                    Forms\Components\TextInput::make('company')->label('Company')->required(),
                    Forms\Components\Select::make('type_id')
                        ->relationship('type', 'type')
                        ->label('Type')->required(),
                ])->columns(2),
            ])->columns(3);
    }

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

    public static function getRelations(): array
    {
        return [
            AccountsRelationManager::class,
            BranchesRelationManager::class,
            TransmittalsRelationManager::class,
            SalesRelationManager::class,
            ExpensesRelationManager::class,
            JournalsRelationManager::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;
    }
}
