<?php

namespace App\Http\Controllers;

use App\Models\Transaction;
use App\Models\BankAccount;
use App\Models\DigitalPayment;
use Illuminate\Http\Request;

class AccountsController extends Controller
{
    public function index()
    {
        $user = auth()->user();
        $isSuperAdmin = $user->isSuperAdmin();

        // 🔹 Cash Accounts (from transactions table)
        $cashQuery = Transaction::where('payment_method', 'cash')
            ->selectRaw('
                MIN(id) as id,
                "Cash Wallet" as account_name,
                "Cash" as provider,
                payment_method,
                SUM(CASE WHEN type = "income" THEN amount ELSE 0 END) -
                SUM(CASE WHEN type = "expense" THEN amount ELSE 0 END) as balance,
                COUNT(*) as transaction_count
            ')
            ->groupBy('payment_method');

        if (!$isSuperAdmin) {
            $cashQuery->where('user_id', $user->id);
        }
        $cashAccounts = $cashQuery->get()->map(fn($acc) => (object)[
            'id' => $acc->id,
            'type' => 'cash',
            'account_name' => $acc->account_name,
            'provider' => $acc->provider,
            'balance' => $acc->balance ?? 0,
            'is_active' => true,
            'transaction_count' => $acc->transaction_count ?? 0,
            'route_prefix' => 'transactions',
            'account_number' => null,
            'branch' => null
        ]);

        // 🔹 Bank Accounts
        $bankQuery = BankAccount::withCount('transactions');
        if (!$isSuperAdmin) {
            $bankQuery->where('user_id', $user->id);
        }
        $bankAccounts = $bankQuery->orderBy('is_active', 'desc')
            ->orderBy('bank_name')
            ->get()
            ->map(fn($acc) => (object)[
                'id' => $acc->id,
                'type' => 'bank',
                'account_name' => $acc->account_name,
                'provider' => $acc->bank_name,
                'balance' => $acc->balance,
                'is_active' => $acc->is_active,
                'transaction_count' => $acc->transactions_count,
                'route_prefix' => 'bank-accounts',
                'account_number' => $acc->account_number,
                'branch' => $acc->branch
            ]);

        // 🔹 Digital Payment Accounts
        $digitalQuery = DigitalPayment::withCount('transactions');
        if (!$isSuperAdmin) {
            $digitalQuery->where('user_id', $user->id);
        }
        $digitalAccounts = $digitalQuery->orderBy('is_active', 'desc')
            ->orderBy('provider')
            ->get()
            ->map(fn($acc) => (object)[
                'id' => $acc->id,
                'type' => 'digital',
                'account_name' => $acc->account_name,
                'provider' => $acc->provider,
                'balance' => $acc->balance ?? 0,
                'is_active' => $acc->is_active,
                'transaction_count' => $acc->transactions_count,
                'route_prefix' => 'digital-payments',
                'account_number' => $acc->account_number,
                'branch' => null
            ]);

        // 🔹 Combine all accounts
        $allAccounts = $cashAccounts->concat($bankAccounts)->concat($digitalAccounts)
            ->sortByDesc('is_active')
            ->sortBy('provider');

        return view('accounts.all', compact('allAccounts'));
    }
}
