<?php
// declare(strict_types=1);
class Customers{
    public function __construct(protected Database $database,protected string $table)
    {
    }

    function validate(){

    }
    public function maxID():int
    {
        $sql = "SELECT max(cust_id) as maxID from {$this->table}";

        $conn = $this->database->getConnection();

        $stmt = $conn->prepare($sql);
        $stmt->execute();
        $row = $stmt->fetch();

        return $row['maxID']+1;
    }
public function findCustVat(int $cust_vat):bool|array
{
    $sql = "SELECT * from {$this->table} where cust_vat = ?";
    

        $conn = $this->database->getConnection();

        $stmt = $conn->prepare($sql);
        $stmt->bindParam(1,$cust_vat ,PDO::PARAM_INT);
        $stmt->execute();
        if($stmt->rowCount() > 0){
            return $stmt->fetch();
        }else{
            return false;
        }
}
public function singleCust(int $cust_id):bool|array
{
    $sql = "SELECT * from {$this->table} where cust_id = ?";
    

        $conn = $this->database->getConnection();

        $stmt = $conn->prepare($sql);
        $stmt->bindParam(1,$cust_id ,PDO::PARAM_INT);
        $stmt->execute();
        if($stmt->rowCount() > 0){
            return $stmt->fetch();
        }else{
            return false;
        }
}
public function findIDSA(int $idSA):bool|array
{
    $sql = "SELECT * from {$this->table} where idSA = ?";
    
        $conn = $this->database->getConnection();
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(1,$idSA ,PDO::PARAM_INT);
        $stmt->execute();
        if($stmt->rowCount() > 0){
            return $stmt->fetch();
        }else{
            return false;
        }
}
public function findPhone(int $phone):bool|array
{
    $sql = "SELECT * from {$this->table} where phone = ?";
    
        $conn = $this->database->getConnection();
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(1,$phone ,PDO::PARAM_INT);
        $stmt->execute();
        if($stmt->rowCount() > 0){
            return $stmt->fetch();
        }else{
            return false;
        }
}
public function insert(array $data): bool
    {
        $data['cust_id'] = $this->maxID();
        if($data['cust_photo'] !=""){
            $ext = explode('.', $data['cust_photo']);
            $data['cust_photo'] =  $data['cust_id']. '.' . $ext[1];
        }

        $columns = implode(", ", array_keys($data));
        $placeholders = implode(", ", array_fill(0, count($data), "?"));

        $sql = "INSERT INTO {$this->table} ($columns)
                VALUES ($placeholders)";

        $conn = $this->database->getConnection();

        $stmt = $conn->prepare($sql);

        $i = 1;

        foreach ($data as $value) {
            
            $type = match(gettype($value)) {
                "boolean" => PDO::PARAM_BOOL,
                "integer" => PDO::PARAM_INT,
                "NULL" => PDO::PARAM_NULL,
                default => PDO::PARAM_STR
            };
            $stmt->bindValue($i++, $value, $type);
        }
        return $stmt->execute();
    }//end insert compnay

    public function insertCustomer(array $data): bool
    {
         $conn = $this->database->getConnection();
         $maxD = $this->maxID();

        $data['cust_id'] = $maxD ;
        if($data['idSA'] >  2000000000){
            unset($data['birth_hijri']);
             } else {
            unset($data['birth']);}
        $columns = implode(", ", array_keys($data));
        $placeholders = implode(", ", array_fill(0, count($data), "?"));

        $sql = "INSERT INTO {$this->table} ($columns)
                VALUES ($placeholders)";

        $conn = $this->database->getConnection();

        $stmt = $conn->prepare($sql);

        $i = 1;

        foreach ($data as $value) {
            
            $type = match(gettype($value)) {
                "boolean" => PDO::PARAM_BOOL,
                "integer" => PDO::PARAM_INT,
                "NULL" => PDO::PARAM_NULL,
                default => PDO::PARAM_STR
            };
            $stmt->bindValue($i++, $value, $type);
        }

     return ($stmt->execute());
    }
    // end insert customer
    //
public function update(string $cust_id, array $data): bool
    {
        // $this->validate($data);

        // if ( ! empty($this->errors)) {
        //     return false;
        // }
        
        $sql = "UPDATE {$this->table} ";

        unset($data["cust_id"]);

        $assignments = array_keys($data);

        array_walk($assignments, function (&$value) {
            $value = "$value = ?";
        });

        $sql .= " SET " . implode(", ", $assignments);

        $sql .= " WHERE cust_id = ? ";
        $conn = $this->database->getConnection();
        $stmt = $conn->prepare($sql);
        $i = 1;

        foreach ($data as $value) {

            $type = match(gettype($value)) {
                "boolean" => PDO::PARAM_BOOL,
                "integer" => PDO::PARAM_INT,
                "NULL" => PDO::PARAM_NULL,
                default => PDO::PARAM_STR
            };

            $stmt->bindValue($i++, $value, $type);

        }

        $stmt->bindValue($i, $cust_id, PDO::PARAM_INT);

        return $stmt->execute();        
    }

}
?>