<?php

namespace App\Repositories;

use App\Config\Database;

class BrandRepository extends BaseRepository
{

    /**
     * Get all allowed fields
     * 
     * @return array
     */
    protected function getAllowedFields(): array
    {
        return [
            'id_brand'         => 'id_brand',
            'name_brand'       => 'name_brand',
            'reference_brand'  => 'reference_brand',
            'is_active'        => 'is_active',
            'created_at'       => 'created_at',
            'updated_at'       => 'updated_at'
        ];
    }

    /**
     * Get all brands by params
     * 
     * @throws Exception
     * 
     * @return array
     * 
     * @param array $params
     */
    public function getAllBrands(array $params): array
    {
        return $this->getAllFromTable('brands', $params);
    }

    /**
     * Get brand by id
     * 
     * @throws Exception
     * 
     * @return array|null
     * 
     * @param int $id
     */
    public function getById(int $id): ?array
    {
        $sql = "
            SELECT 
                id_brand,
                name_brand,
                is_active
            FROM brands
            WHERE id_brand = ?
            LIMIT 1
        ";

        $conn = Database::connection();

        $stmt = $conn->prepare($sql);
        $stmt->bind_param("i", $id);
        $stmt->execute();

        $result = $stmt->get_result()->fetch_assoc();

        return $result ?: null;
    }
}
