<?php

require_once __DIR__ . '/constants.php';

class Database
{
    private $host = "localhost";
    private $db_name = "service_marketplace";
    private $username = "root";
    private $password = "";
    private $conn;

    public function connect()
    {
        $this->conn = null;

        try {

            $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8mb4";

            $this->conn = new PDO($dsn, $this->username, $this->password, [
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                PDO::ATTR_PERSISTENT => false
            ]);

        } catch(PDOException $e) {

            if (ENVIRONMENT === 'development') {
                die("Database Connection Error: " . $e->getMessage());
            } else {
                die("Database connection failed.");
            }

        }

        return $this->conn;
    }
}