<?php
class ProfileImg {

    // database connection
    private $conn;
    
    public $profileImg;
    public $oldProfileImg;
    public $accType;
    public $username;
    
    // constructor with $db as database connection
    public function __construct($db){
        $this->conn = $db;
    }
    
    public function getConnection() {
        return $this->conn;
    }

    function uploadProfileImg() {
        
        if (!isset($this->profileImg['name']) || !isset($this->profileImg['tmp_name'])) {
            return false; // Invalid file upload data
        }
        
        // Generate a random filename
        $randomFilename = $this->generateRandomFilename() . '.' . pathinfo($this->profileImg['name'], PATHINFO_EXTENSION);
        
        // Define the path for the upload
        $upload_dir = 'images/';
        $upload_file = $upload_dir . $randomFilename;
        
        
        // Move the uploaded file to the desired location
        if (move_uploaded_file($this->profileImg['tmp_name'], $upload_file)) {
            
            // Determine the table based on account type
            switch ($this->accType) {
                case 'coach':
                    $query = "UPDATE coachProfile SET profileImg = ? WHERE username = ?";
                    break;
                case 'customer':
                    $query = "UPDATE customerProfile SET profileImg = ? WHERE username = ?";
                    break;
                case 'client':
                    $query = "UPDATE clientProfile SET profileImg = ? WHERE username = ?";
                    break;
                default:
                    return false; // Unsupported account type
            }
            
            // Prepare query statement
            $stmt = $this->conn->prepare($query);
            if ($stmt === false) {
                return false; // Prepare failed
            }
            
            // Sanitize
            $this->username = htmlspecialchars(strip_tags($this->username));
            $randomFilename = htmlspecialchars(strip_tags($randomFilename));
            
            // Bind new values
            $stmt->bind_param('ss', $randomFilename, $this->username);
            
            // Execute the query
            if ($stmt->execute()) {
                return $randomFilename;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    
    function updateProfileImg() {
        
        if (!empty($this->accType)) {
            // Retrieve the current profile image path
            $query = "SELECT profileImg FROM " . $this->getProfileTable() . " WHERE username = ?";
            $stmt = $this->conn->prepare($query);
            $stmt->bind_param('s', $this->username);
            $stmt->execute();
            $stmt->bind_result($oldProfileImg);
            $stmt->fetch();
            $stmt->close();
            
            // Delete the old profile image file if it exists
            if ($oldProfileImg && file_exists('images/' . $oldProfileImg)) {
                unlink('images/' . $oldProfileImg);
            }
            
            if (!isset($this->profileImg['name']) || !isset($this->profileImg['tmp_name'])) {
                return false; // Invalid file upload data
            }
            
            // Generate a random filename
            $randomFilename = $this->generateRandomFilename() . '.' . pathinfo($this->profileImg['name'], PATHINFO_EXTENSION);
            
            // Define the path for the upload
            $upload_dir = 'images/';
            $upload_file = $upload_dir . $randomFilename;
            
            // Move the uploaded file to the desired location
            if (move_uploaded_file($this->profileImg['tmp_name'], $upload_file)) {
                
                $query = "UPDATE " . $this->getProfileTable() . " SET profileImg = ? WHERE username = ?";
                
                // Prepare query statement
                $stmt = $this->conn->prepare($query);
                if ($stmt === false) {
                    return false; // Prepare failed
                }
                
                // Sanitize
                $this->username = htmlspecialchars(strip_tags($this->username));
                $randomFilename = htmlspecialchars(strip_tags($randomFilename));
                // Bind new values
                $stmt->bind_param('ss', $randomFilename, $this->username);
                
                // Execute the query
                if ($stmt->execute()) {
                    return $randomFilename;
                }else{
                    return false;
                }
                
            } else{
                return false;
            }
        }else{
            return false;
        }
    }

    function generateRandomFilename($length = 128) {
        return bin2hex(random_bytes($length / 2));
    }
    
    function getAccType() {
        $query = "SELECT accType FROM idList WHERE username= '".$this->username."' ";
        $result = mysqli_query($this->conn, $query);
        if ($row = mysqli_fetch_assoc($result)) {
            return $row['accType'];
            
        }
        return;
    }
    
    function getProfileTable() {
        $table = false;
        switch ($this->accType) {
            case 'coach':
                $table = 'coachProfile';
                break;
            case 'customer':
                $table = 'customerProfile';
                break;
            default:
                $table = 'clientProfile';
                break;
        }
        return $table;
    }
}

?>