<?php

namespace App\Models;

use CodeIgniter\Model;

class TurbineModel extends Model
{
    protected $table         = 'turbines';
    protected $primaryKey    = 'id';
    protected $returnType    = 'array';
    protected $useTimestamps = true;
    protected $allowedFields = [
        'site_id',
        'turbine_code',
        'name',
        'manufacturer',
        'model',
        'serial_number',
        'rated_power_kw',
        'hub_height_m',
        'rotor_diameter_m',
        'commissioned_at',
        'status',
    ];

    public function withRelations()
    {
        return $this->select(
            'turbines.*, sites.name AS site_name, sites.site_code, sites.organization_id, organizations.name AS organization_name'
        )
            ->join('sites', 'sites.id = turbines.site_id')
            ->join('organizations', 'organizations.id = sites.organization_id');
    }

    public function forOrganization(int $organizationId): array
    {
        return $this->withRelations()
            ->where('sites.organization_id', $organizationId)
            ->orderBy('sites.name', 'ASC')
            ->orderBy('turbines.turbine_code', 'ASC')
            ->findAll();
    }
}
