<?php

namespace App\Models;

use CodeIgniter\Model;

class OrganizationModel extends Model
{
    protected $table            = 'organizations';
    protected $primaryKey       = 'id';
    protected $returnType       = 'array';
    protected $useTimestamps    = true;
    protected $allowedFields    = [
        'name',
        'slug',
        'organization_type',
        'country_code',
        'email',
        'website',
        'status',
    ];

    public function forUser(int $userId): array
    {
        return $this->select('organizations.*')
            ->join('organization_memberships', 'organization_memberships.organization_id = organizations.id')
            ->where('organization_memberships.user_id', $userId)
            ->where('organization_memberships.status', 'active')
            ->orderBy('organizations.name', 'ASC')
            ->findAll();
    }
}
