<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\CargoItem;
use App\Models\City;
use App\Models\Contract;
use App\Models\FinancialCoefficient;
use App\Models\FinancialItem;
use App\Models\FleetType;
use App\Models\RouteType;
use App\Models\TransportRoute;

class ReferenceDataController extends Controller
{
    public function index()
    {
        $cities = City::query()->where('is_active', true)->get();
        $loaderTypes = FleetType::query()->where('is_active', true)->get();
        $cargoTypes = CargoItem::query()->where('is_active', true)->get();
        $routeTypes = RouteType::query()->where('is_active', true)->get();
        $routes = TransportRoute::query()->where('is_active', true)->with(['originCity', 'destinationCity', 'routeType'])->get();
        $coefficients = FinancialCoefficient::query()->where('is_active', true)->orderBy('coefficient_group')->orderBy('sort_order')->get();
        $financialItems = FinancialItem::query()->where('is_active', true)->get();

        return response()->json([
            'success' => true,
            'message' => 'Reference data retrieved successfully',
            'data' => [
                'cities' => $cities,
                'loader_types' => $loaderTypes,
                'cargo_types' => $cargoTypes,
                'route_types' => $routeTypes,
                'routes' => $routes,
                'financial_coefficients' => $coefficients,
                'financial_items' => $financialItems,
                'standard_goods_codes' => $cargoTypes->pluck('code')->values(),
                'contracts' => Contract::query()->where('is_active', true)->get(),
            ],
        ]);
    }
}
