<?php

namespace App\Services\ClassPreparation;

use App\Models\ClassPrepAdjustment;

class ClassPreparationAdjustmentService
{
    public function applyAdjustments(array $baseItems, string $classSectionId, string $schoolYear): array
    {
        $rawAdjustments = ClassPrepAdjustment::query()
            ->where('class_section_id', $classSectionId)
            ->where('school_year', $schoolYear)
            ->where('adjustable', 1)
            ->get()
            ->toArray();

        $adjMap = ['Large Table' => 0, 'Small Table' => 0];
        foreach ($rawAdjustments as $adjustment) {
            $item = $adjustment['item_name'] ?? '';
            $delta = (int) ($adjustment['adjustment'] ?? 0);
            if (array_key_exists($item, $adjMap)) {
                $adjMap[$item] = $delta;
            }
            if (isset($baseItems[$item])) {
                $baseItems[$item] = max(0, (int) $baseItems[$item] + $delta);
            }
        }

        return [$baseItems, $adjMap];
    }
}
