<?php

declare(strict_types=1);

namespace App\Models;

use App\Core\Model;
use PDO;

/**
 * Affiliation Model
 *
 * Handles affiliations and partners data retrieval.
 */
class Affiliation extends Model
{
    /**
     * The database table associated with the model.
     */
    protected string $table = 'affiliations';

    /**
     * Get affiliations/partners filtered by type.
     *
     * @param string $type The type to filter by ('affiliation' or 'partner')
     * @return array<int, array<string, mixed>>
     */
    public function getByType(string $type): array
    {
        $sql = sprintf(
            'SELECT * FROM `%s` WHERE `type` = :type AND `active` = :active ORDER BY `sort_order` ASC',
            $this->table
        );

        $stmt = $this->db->prepare($sql);
        $stmt->execute([':type' => $type, ':active' => 1]);

        return $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
    }

    /**
     * Get all active affiliations and partners ordered by sort_order.
     *
     * @return array<int, array<string, mixed>>
     */
    public function getActive(): array
    {
        $sql = sprintf(
            'SELECT * FROM `%s` WHERE `active` = :active ORDER BY `sort_order` ASC',
            $this->table
        );

        $stmt = $this->db->prepare($sql);
        $stmt->execute([':active' => 1]);

        return $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
    }
}
