<?php

namespace Core\App\Models;

class AdminProfileModel extends BaseModel
{
    protected string $table = 'admins';

    public function findById(int $id): ?array
    {
        return $this->fetchOne(
            "SELECT * FROM {$this->table} WHERE id = :id LIMIT 1",
            ['id' => $id]
        );
    }

    public function updateProfile(
        int $id,
        string $username,
        ?string $email,
        ?string $avatar,
        ?string $phone = null
    ): bool {
        return $this->execute(
            "UPDATE {$this->table}
             SET username = :username,
                 email    = :email,
                 avatar   = :avatar,
                 phone    = :phone
             WHERE id = :id",
            [
                'id'       => $id,
                'username' => $username,
                'email'    => $email,
                'avatar'   => $avatar,
                'phone'    => $phone !== '' ? $phone : null,
            ]
        );
    }

    public function updatePassword(int $id, string $password): bool
    {
        return $this->execute(
            "UPDATE {$this->table}
             SET password = :password
             WHERE id = :id",
            [
                'id'       => $id,
                'password' => password_hash($password, PASSWORD_DEFAULT)
            ]
        );
    }
}
