<?php

namespace App\Services\Coach;

use App\Models\AuditLog;
use App\Models\ForbiddenFood;
use App\Models\NutritionDay;
use App\Models\NutritionItem;
use App\Models\NutritionMeal;
use App\Models\NutritionPlan;
use App\Models\Supplement;

class NutritionBuilderService
{
    // --- روزها ---

    public function addDay(NutritionPlan $plan, string $label): NutritionDay
    {
        $nextOrder = $plan->days()->max('sort_order') + 1;

        return NutritionDay::create([
            'nutrition_plan_id' => $plan->id,
            'label' => $label,
            'sort_order' => $nextOrder,
        ]);
    }

    public function deleteDay(NutritionDay $day): void
    {
        $day->delete();
    }

    public function moveDay(NutritionDay $day, int $direction): void
    {
        $this->moveSortOrder($day, $day->plan->days(), $direction);
    }

    // --- وعده‌ها ---

    public function addMeal(NutritionDay $day, array $data): NutritionMeal
    {
        $nextOrder = $day->meals()->max('sort_order') + 1;

        return NutritionMeal::create([
            'nutrition_day_id' => $day->id,
            'name' => $data['name'],
            'consumption_time' => $data['consumption_time'] ?? null,
            'sort_order' => $nextOrder,
        ]);
    }

    public function updateMeal(NutritionMeal $meal, array $data): void
    {
        $meal->update([
            'name' => $data['name'],
            'consumption_time' => $data['consumption_time'] ?? null,
        ]);
    }

    public function deleteMeal(NutritionMeal $meal): void
    {
        $meal->delete();
    }

    // --- اقلام غذایی ---

    public function addItem(NutritionMeal $meal, array $data): NutritionItem
    {
        $nextOrder = $meal->items()->max('sort_order') + 1;

        $item = NutritionItem::create([
            'nutrition_meal_id' => $meal->id,
            'food_name' => $data['food_name'],
            'amount' => $data['amount'] ?? null,
            'unit' => $data['unit'] ?? 'gram',
            'calories' => $data['calories'] ?? null,
            'notes' => $data['notes'] ?? null,
            'sort_order' => $nextOrder,
        ]);

        AuditLog::record('nutrition_item.created', $item);

        return $item;
    }

    public function updateItem(NutritionItem $item, array $data): void
    {
        $item->update([
            'food_name' => $data['food_name'],
            'amount' => $data['amount'] ?? null,
            'unit' => $data['unit'] ?? 'gram',
            'calories' => $data['calories'] ?? null,
            'notes' => $data['notes'] ?? null,
        ]);
    }

    public function deleteItem(NutritionItem $item): void
    {
        $item->delete();
    }

    // --- مواد ممنوع ---

    public function addForbiddenFood(NutritionPlan $plan, array $data): ForbiddenFood
    {
        return ForbiddenFood::create([
            'nutrition_plan_id' => $plan->id,
            'food_name' => $data['food_name'],
            'notes' => $data['notes'] ?? null,
        ]);
    }

    public function deleteForbiddenFood(ForbiddenFood $food): void
    {
        $food->delete();
    }

    // --- مکمل‌ها ---

    public function addSupplement(NutritionPlan $plan, array $data): Supplement
    {
        return Supplement::create([
            'nutrition_plan_id' => $plan->id,
            'name' => $data['name'],
            'amount' => $data['amount'] ?? null,
            'consumption_time' => $data['consumption_time'] ?? null,
            'notes' => $data['notes'] ?? null,
        ]);
    }

    public function deleteSupplement(Supplement $supplement): void
    {
        $supplement->delete();
    }

    // --- کمکی ---

    private function moveSortOrder($model, $siblingsQuery, int $direction): void
    {
        $siblings = $siblingsQuery->orderBy('sort_order')->get();
        $index = $siblings->search(fn ($s) => $s->id === $model->id);
        $swapIndex = $index + $direction;

        if (! isset($siblings[$swapIndex])) {
            return;
        }

        $a = $siblings[$index];
        $b = $siblings[$swapIndex];

        [$a->sort_order, $b->sort_order] = [$b->sort_order, $a->sort_order];
        $a->save();
        $b->save();
    }
}
