<?php

namespace App\Modules\Auth\Repositories;

use Core\Database\DB;
use PDO;

class AuthRepository
{
    public function findByEmail(string $email): ?array
    {
        $stmt = DB::conn()->prepare("
            SELECT *
            FROM users
            WHERE email = :email
            LIMIT 1
        ");

        $stmt->execute([
            'email' => $email
        ]);

        $user = $stmt->fetch(PDO::FETCH_ASSOC);

        return $user ?: null;
    }

    public function create(array $data): bool
    {
        $stmt = DB::conn()->prepare("
            INSERT INTO users (
                name,
                gender,
                date_of_birth,
                email,
                phone,
                password,
                role,
                avatar,
                verification_status,
                verification_note,
                created_at
            ) VALUES (
                :name,
                :gender,
                :date_of_birth,
                :email,
                :phone,
                :password,
                :role,
                :avatar,
                :verification_status,
                :verification_note,
                NOW()
            )
        ");

        return $stmt->execute([
            'name' => $data['name'],
            'gender' => $data['gender'],
            'date_of_birth' => $data['date_of_birth'],
            'email' => $data['email'],
            'phone' => $data['phone'],
            'password' => password_hash($data['password'], PASSWORD_DEFAULT),
            'role' => $data['role'],
            'avatar' => $data['avatar'],
            'verification_status' => $data['verification_status'],
            'verification_note' => $data['verification_note'],
        ]);
    }
}