<?php

namespace App\Http\Controllers\Ranch;

use App\Http\Controllers\Controller;
use App\Models\Cattle;
use App\Models\FatteningLot;
use App\Models\MaterialExpense;
use App\Models\CattleCalvingEvent;
use App\Models\CattleSaleIncome;
use App\Services\RanchAlertCenterService;
use App\Services\RanchInventoryReportService;
use App\Services\RanchOpenLotsGdpAlertService;
use App\Services\RanchOpenLotsProjectionService;
use App\Services\RanchHealthReportService;
use App\Services\WeightPerformanceReportService;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;

class RanchDashboardController extends Controller
{
    public function __construct(
        private readonly WeightPerformanceReportService $weightPerformanceService,
        private readonly RanchHealthReportService $healthReportService,
        private readonly RanchAlertCenterService $alertCenterService,
        private readonly RanchInventoryReportService $inventoryReportService,
        private readonly RanchOpenLotsProjectionService $openLotsProjectionService,
        private readonly RanchOpenLotsGdpAlertService $openLotsGdpAlertService,
    ) {}

    public function summary(): JsonResponse
    {
        $monthStart = Carbon::now()->startOfMonth();
        $monthEnd = Carbon::now()->endOfMonth();

        $weightAlerts = $this->weightPerformanceService->generate();
        $healthSummary = $this->healthReportService->summary($monthStart, $monthEnd);
        $alertCenter = $this->alertCenterService->generate();

        return response()->json([
            'data' => [
                'active_cattle' => Cattle::active()->count(),
                'sold_this_month' => Cattle::where('status', Cattle::STATUS_SOLD)
                    ->whereBetween('deactivated_at', [$monthStart, $monthEnd])->count(),
                'deaths_this_month' => Cattle::where('status', Cattle::STATUS_DEAD)
                    ->whereBetween('deactivated_at', [$monthStart, $monthEnd])->count(),
                'open_fattening_lots' => FatteningLot::open()->count(),
                'expenses_this_month' => (float) MaterialExpense::whereBetween('expense_date', [$monthStart, $monthEnd])->sum('total_cost'),
                'income_this_month' => (float) CattleSaleIncome::whereBetween('sale_date', [$monthStart, $monthEnd])->sum('total_price'),
                'weight_alerts_count' => $weightAlerts['alert_count'],
                'health_events_this_month' => $healthSummary['total_events'],
                'illness_events_this_month' => $healthSummary['illness_count'],
                'at_risk_mothers_count' => count($healthSummary['at_risk_mothers']),
                'alert_center_total' => $alertCenter['total_count'],
                'inventory_by_location' => $this->inventoryReportService->activeLocationSummary(8),
                'open_fattening_lots_below_target' => $this->openLotsProjectionService->countBelowTarget(),
                'open_lots_gdp_alerts' => $this->openLotsGdpAlertService->lotsWithAlertsCount(),
                'calvings_this_month' => CattleCalvingEvent::whereBetween('calving_date', [$monthStart, $monthEnd])->count(),
            ],
        ]);
    }
}
