<?php

namespace App\Http\Controllers\Ranch;

use App\Http\Controllers\Controller;
use App\Services\RanchCalvingReportNotificationService;
use App\Services\RanchCostAlertNotificationService;
use App\Services\RanchOpenLotsGdpAlertNotificationService;
use App\Services\RanchWeightAlertNotificationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class RanchNotificationController extends Controller
{
    public function __construct(
        private readonly RanchWeightAlertNotificationService $notificationService,
        private readonly RanchCostAlertNotificationService $costNotificationService,
        private readonly RanchOpenLotsGdpAlertNotificationService $openLotsGdpNotificationService,
        private readonly RanchCalvingReportNotificationService $calvingReportNotificationService,
    ) {}

    public function sendCalvingReport(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'from' => ['nullable', 'date'],
            'to' => ['nullable', 'date', 'after_or_equal:from'],
            'pasture_id' => ['nullable', 'integer', 'exists:pastures,id'],
            'mother_id' => ['nullable', 'integer', 'exists:cattle,id'],
            'previous_month' => ['nullable', 'boolean'],
        ]);

        $filters = array_filter([
            'from' => $validated['from'] ?? null,
            'to' => $validated['to'] ?? null,
            'pasture_id' => $validated['pasture_id'] ?? null,
            'mother_id' => $validated['mother_id'] ?? null,
        ], fn ($v) => $v !== null);

        $usePreviousMonth = (bool) ($validated['previous_month'] ?? false)
            && ! isset($filters['from']) && ! isset($filters['to']);

        $result = $this->calvingReportNotificationService->send($filters, $usePreviousMonth);

        return response()->json(['data' => $result]);
    }

    public function sendOpenLotsGdpAlerts(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'min_gdp' => ['nullable', 'numeric', 'min:0'],
            'days_without_weigh' => ['nullable', 'integer', 'min:1', 'max:365'],
            'force' => ['nullable', 'boolean'],
        ]);

        $result = $this->openLotsGdpNotificationService->sendIfNeeded(
            isset($validated['min_gdp']) ? (float) $validated['min_gdp'] : null,
            isset($validated['days_without_weigh']) ? (int) $validated['days_without_weigh'] : null,
            (bool) ($validated['force'] ?? false),
        );

        return response()->json(['data' => $result]);
    }

    public function sendWeightAlerts(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'min_gdp' => ['nullable', 'numeric', 'min:0'],
            'days_without_weigh' => ['nullable', 'integer', 'min:1', 'max:365'],
            'force' => ['nullable', 'boolean'],
        ]);

        $result = $this->notificationService->sendIfNeeded(
            isset($validated['min_gdp']) ? (float) $validated['min_gdp'] : null,
            isset($validated['days_without_weigh']) ? (int) $validated['days_without_weigh'] : null,
            (bool) ($validated['force'] ?? false),
        );

        return response()->json(['data' => $result]);
    }

    public function sendCostAlerts(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'max_cost_per_kg' => ['nullable', 'numeric', 'min:0'],
            'min_weight_gain_kg' => ['nullable', 'numeric', 'min:0'],
            'force' => ['nullable', 'boolean'],
        ]);

        $result = $this->costNotificationService->sendIfNeeded(
            isset($validated['max_cost_per_kg']) ? (float) $validated['max_cost_per_kg'] : null,
            isset($validated['min_weight_gain_kg']) ? (float) $validated['min_weight_gain_kg'] : null,
            (bool) ($validated['force'] ?? false),
        );

        return response()->json(['data' => $result]);
    }
}
