<?php namespace App\Models;

use App\Entities\Pet\Pet;

class PetModel extends ModelEntity
{
    protected $table = 'pet';
    protected $primaryKey = 'id';
    protected $returnType = 'array';
    protected $allowedFields = [
        'data_nascimento',
        'id_raca',
        'cor',
        'id_pet_status',
        'sexo',
        'castrado',
        'numero_chip',
        'rga_pedigree',
        'doado',
        'nome',
        'porte',
        'observacoes',
        'peso'
    ];
    protected $useSoftDeletes = false;
    protected $skipValidation = true;
    protected $useTimestamps = false;

    public function registerPet (Pet &$pet) : void {
        $pet_data = $pet->toArray();
        unset($pet_data['id']);
        $id_new_pet = $this->insert($pet_data);
        $pet->setId($id_new_pet);

    }

    public function updatePetByID (int $id_pet, Pet $pet) : bool {
        $data = $pet->toArray(true);
        if (count($data) > 0) {
            $binds = ['id_pet' => $id_pet];
            $sql = "
                UPDATE pet
            ";
            $sql .= $this->generateUpdateSets($data, $binds);
            $sql .= 'WHERE pet.id = :id_pet:';
            $query = $this->db->query($sql, $binds);
            $errors = $this->errors();
            return !$errors;
        } else {
            return true;
        }
    }
}
