<?php

namespace App\Filament\App\Widgets;

use App\Models\FollowUp;
use App\Models\OpeningBalance;
use App\Models\Transaction;
use Filament\Facades\Filament;
use Filament\Widgets\StatsOverviewWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class CustomerOverview extends StatsOverviewWidget
{
    protected int|string|array $columnSpan = 12;

    protected function getStats(): array
    {
        $user = Filament::auth()->user();

        $todayFollowUps = FollowUp::query()
            ->where('user_id', $user->id)
            ->whereDate('followup_date', today())
            ->where('status', 'pending')
            ->count();

        $overdueFollowUps = FollowUp::query()
            ->where('user_id', $user->id)
            ->where('status', 'pending')
            ->where('followup_date', '<', now())
            ->count();

        $receivables = (float) Transaction::query()
            ->where('user_id', $user->id)
            ->where('type', 'receivable')
            ->where('status', 'pending')
            ->sum('amount');

        $payables = (float) Transaction::query()
            ->where('user_id', $user->id)
            ->where('type', 'payable')
            ->where('status', 'pending')
            ->sum('amount');

        $income = (float) Transaction::query()
            ->where('user_id', $user->id)
            ->whereIn('type', ['income'])
            ->sum('amount');

        $expenses = (float) Transaction::query()
            ->where('user_id', $user->id)
            ->where('type', 'expense')
            ->sum('amount');

        $opening = (float) OpeningBalance::query()
            ->where('user_id', $user->id)
            ->sum('amount');

        $estimatedBalance = $opening + $income + $receivables - $expenses - $payables;
        $monthlyExpenses = (float) Transaction::query()
            ->where('user_id', $user->id)
            ->where('type', 'expense')
            ->whereBetween('date', [now()->startOfMonth()->toDateString(), now()->endOfMonth()->toDateString()])
            ->sum('amount');

        return [
            Stat::make('Today Follow-ups', (string) $todayFollowUps)
                ->description('Calls and reminders due today')
                ->color('info'),
            Stat::make('Overdue', (string) $overdueFollowUps)
                ->description('Pending follow-ups needing action')
                ->color($overdueFollowUps > 0 ? 'danger' : 'success'),
            Stat::make('Receivables', 'Rs. ' . number_format($receivables, 2))
                ->description('Money expected back')
                ->color('success'),
            Stat::make('Payables', 'Rs. ' . number_format($payables, 2))
                ->description('Money to be paid')
                ->color('warning'),
            Stat::make('This Month Spend', 'Rs. ' . number_format($monthlyExpenses, 2))
                ->description('Recorded spending this month')
                ->color('danger'),
            Stat::make('Balance Est.', 'Rs. ' . number_format($estimatedBalance, 2))
                ->description('Opening + income + receivables - expenses - payables')
                ->color('primary'),
        ];
    }
}
