<?php

namespace App\Filament\Resources\Clients\Pages;

use App\Filament\Resources\Clients\ClientResource;
use App\Models\Account;
use Filament\Resources\Pages\Page;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\Summarizers\Summarizer;
use Filament\Tables\Columns\Summarizers\Sum;
use Filament\Resources\Pages\Concerns\InteractsWithRecord;
use Illuminate\Database\Eloquent\Builder;

class TrialBalance extends Page implements HasTable
{
    use InteractsWithTable;
    use InteractsWithRecord;

    protected static string $resource = ClientResource::class;

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

    protected string $view = 'filament.resources.client-resource.pages.trial-balance';

    public function mount(int | string $record): void
    {
        $this->record = $this->resolveRecord($record);
    }

    public function table(Table $table): Table
    {
        return $table
            ->query(
                Account::query()
                    ->where('client_id', $this->getRecord()->id)
                    ->with(['accountType', 'latestBalance'])
            )
            ->columns([
                TextColumn::make('account')
                    ->label('Account Name')
                    ->sortable()
                    ->searchable(),
                TextColumn::make('debit')
                    ->label('Debit')
                    ->money('PHP')
                    ->state(function (Account $record) {
                        $balance = $record->latestBalance?->balance ?? 0;
                        $normal = strtolower($record->accountType->normal_balance ?? 'debit');
                        
                        return ($normal == 'debit' && $balance >= 0) || ($normal == 'credit' && $balance < 0) 
                            ? abs($balance) : 0;
                    })
                    ->summarize(Summarizer::make()
                        ->label('Total Debit')
                        ->money('PHP')
                        ->using(function ($query) {
                            return Account::query()
                                ->whereIn('id', $query->clone()->pluck('accounts.id'))
                                ->with(['accountType', 'latestBalance'])
                                ->get()
                                ->sum(function ($record) {
                                    $balance = $record->latestBalance?->balance ?? 0;
                                    $normal = strtolower($record->accountType->normal_balance ?? 'debit');
                                    return ($normal == 'debit' && $balance >= 0) || ($normal == 'credit' && $balance < 0) 
                                        ? abs($balance) : 0;
                                });
                        })
                    ),
                TextColumn::make('credit')
                    ->label('Credit')
                    ->money('PHP')
                    ->state(function (Account $record) {
                        $balance = $record->latestBalance?->balance ?? 0;
                        $normal = strtolower($record->accountType->normal_balance ?? 'debit');
                        
                        return ($normal == 'credit' && $balance >= 0) || ($normal == 'debit' && $balance < 0) 
                            ? abs($balance) : 0;
                    })
                    ->summarize(Summarizer::make()
                        ->label('Total Credit')
                        ->money('PHP')
                        ->using(function ($query) {
                            return Account::query()
                                ->whereIn('id', $query->clone()->pluck('accounts.id'))
                                ->with(['accountType', 'latestBalance'])
                                ->get()
                                ->sum(function ($record) {
                                    $balance = $record->latestBalance?->balance ?? 0;
                                    $normal = strtolower($record->accountType->normal_balance ?? 'debit');
                                    return ($normal == 'credit' && $balance >= 0) || ($normal == 'debit' && $balance < 0) 
                                        ? abs($balance) : 0;
                                });
                        })
                    ),
            ]);
    }
}
