<?php

namespace App\Controllers\Api;

use App\Models\InventoryModel;
use App\Models\ProviderModel;

class GasSizesController extends BaseApiController
{
    /**
     * GET api/gas-sizes - list gas sizes with prices (from inventory item_type=gas_size)
     * Optional: ?provider_id= for a specific gas provider
     */
    public function index()
    {
        $providerId = $this->request->getGet('provider_id');
        $inv = model(InventoryModel::class);
        $inv->where('item_type', 'gas_size');
        if ($providerId !== null && $providerId !== '') {
            $inv->where('provider_id', (int) $providerId);
        }
        $rows = $inv->findAll();
        $prov = model(ProviderModel::class);
        $data = [];
        foreach ($rows as $r) {
            $p = $prov->find($r->provider_id);
            $priceKit = isset($r->price_kit) && $r->price_kit !== null && $r->price_kit !== '' ? (float) $r->price_kit : null;
            $data[] = [
                'id'          => (int) $r->id,
                'provider_id' => (int) $r->provider_id,
                'provider_name' => $p ? $p->name : '',
                'item_name'   => $r->item_name,
                'price'       => (float) $r->price,
                'price_kit'   => $priceKit,
                'image_url'   => isset($r->image_url) && $r->image_url !== '' ? $r->image_url : null,
            ];
        }
        return $this->success($data);
    }
}
