<?php

namespace App\Http\Controllers\admin;

use App\Constants\HttpStatus;
use App\Http\Controllers\Controller;
use App\Services\FundsSummaryService;
use Exception;
use Illuminate\Validation\ValidationException;

class Funds extends Controller
{
    public function summary()
    {
        try {
            $params = request()->validate([
                'start_date' => ['nullable', 'date_format:Y-m-d', 'required_with:end_date'],
                'end_date' => ['nullable', 'date_format:Y-m-d', 'required_with:start_date', 'after_or_equal:start_date'],
            ]);
            $today = date('Y-m-d');
            $startDate = $params['start_date'] ?? $today;
            $endDate = $params['end_date'] ?? $today;

            return $this->success(FundsSummaryService::summary($startDate, $endDate));
        } catch (ValidationException $e) {
            return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
        } catch (Exception $e) {
            return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
        }
    }
}
