<?php

namespace App\Http\Controllers\Admin;

use App\Models\Balance;
use App\Utils\Helper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Core\BaseResourceController;

class ReportController extends BaseResourceController
{
  protected $model = Balance::class;

  protected function indexQuery($query, Request $request)
  {
    $query->with(['department', 'user', 'company']);
    return $query;
  }

  protected function getPage(Request $request, $id = null): array
  {
    return [
      "name" => "reports",
      "inertia" => "Admin/Report",
      "label" => "Summary Report",
      "url" => "/admin/reports",
      "fields" => Helper::getFormFields($this->validation($request)),
    ];
  }

  protected function validation(Request $request, $id = null): array
  {
    return [
      "validation" => [],
      "default" => [],
    ];
  }

  protected function indexData(Request $request, $isFormData = true): array
  {
    $summary = DB::table('balances')
      ->select([
        DB::raw('COALESCE(SUM(debit_amount), 0) as total_debit'),
        DB::raw('COALESCE(SUM(credit_amount), 0) as total_credit'),
        DB::raw('COALESCE(SUM(debit_amount), 0) - COALESCE(SUM(credit_amount), 0) as total_remaining'),
      ])
      ->first();

    return [
      'summary' => $summary,
    ];
  }
}
