<?php

namespace App\Models;

use CodeIgniter\Model;

class SiteModel extends Model
{
    protected $table         = 'sites';
    protected $primaryKey    = 'id';
    protected $returnType    = 'array';
    protected $useTimestamps = true;
    protected $allowedFields = [
        'organization_id',
        'name',
        'site_code',
        'site_type',
        'country_code',
        'region',
        'location',
        'latitude',
        'longitude',
        'commissioned_at',
        'status',
    ];

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

    public function withOrganization()
    {
        return $this->select('sites.*, organizations.name AS organization_name, organizations.slug AS organization_slug')
            ->join('organizations', 'organizations.id = sites.organization_id');
    }
}
