<?php

namespace App\Controllers;

use App\Models\MaintenanceTaskModel;
use App\Models\SiteModel;
use App\Models\TurbineModel;

class ApiPlayground extends BaseController
{
    public function index()
    {
        if ($redirect = $this->requireOrganizationContext()) {
            return $redirect;
        }

        $organizationId = (int) $this->currentOrganizationId();
        $sites = (new SiteModel())
            ->where('organization_id', $organizationId)
            ->orderBy('name', 'ASC')
            ->findAll();

        $turbines = (new TurbineModel())
            ->withRelations()
            ->where('sites.organization_id', $organizationId)
            ->orderBy('sites.name', 'ASC')
            ->orderBy('turbines.turbine_code', 'ASC')
            ->findAll();

        $tasks = (new MaintenanceTaskModel())
            ->where('organization_id', $organizationId)
            ->orderBy('name', 'ASC')
            ->findAll();

        return view('api_playground/index', [
            'title' => lang('Backoffice.menuApiPlayground'),
            'active' => 'api_playground',
            'baseApiUrl' => rtrim(site_url('api/v1'), '/'),
            'sites' => $sites,
            'turbines' => $turbines,
            'tasks' => $tasks,
        ]);
    }
}
