<?php

class Timer {
    private $conn;
    private $table_name = 'timers';
    
    public $id;
    public $task_id;
    public $user_id;
    public $start_time;
    public $end_time;
    public $duration;
    public $description;
    public $created_at;
    public $updated_at;
    
    public function __construct($db) {
        $this->conn = $db;
    }
    
    public function create() {
        $query = "INSERT INTO " . $this->table_name . " 
                  SET task_id=:task_id, user_id=:user_id, start_time=:start_time, 
                      end_time=:end_time, duration=:duration, description=:description, 
                      created_at=:created_at, updated_at=:updated_at";
        
        $stmt = $this->conn->prepare($query);
        
        // Handle null values properly
        $this->task_id = $this->task_id ? htmlspecialchars(strip_tags($this->task_id)) : null;
        $this->user_id = htmlspecialchars(strip_tags($this->user_id));
        $this->start_time = htmlspecialchars(strip_tags($this->start_time));
        $this->end_time = $this->end_time ? htmlspecialchars(strip_tags($this->end_time)) : null;
        $this->duration = $this->duration ? htmlspecialchars(strip_tags($this->duration)) : null;
        $this->description = $this->description ? htmlspecialchars(strip_tags($this->description)) : null;
        $this->created_at = gmdate('Y-m-d H:i:s');
        $this->updated_at = gmdate('Y-m-d H:i:s');
        
        $stmt->bindParam(":task_id", $this->task_id);
        $stmt->bindParam(":user_id", $this->user_id);
        $stmt->bindParam(":start_time", $this->start_time);
        $stmt->bindParam(":end_time", $this->end_time);
        $stmt->bindParam(":duration", $this->duration);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":created_at", $this->created_at);
        $stmt->bindParam(":updated_at", $this->updated_at);
        
        if($stmt->execute()) {
            return true;
        }
        
        return false;
    }
    
    public function read() {
        $query = "SELECT t.*, u.first_name, u.last_name 
                  FROM " . $this->table_name . " t 
                  LEFT JOIN users u ON t.user_id = u.id 
                  LEFT JOIN tasks ta ON t.task_id = ta.id 
                  ORDER BY t.created_at DESC";
        
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        
        return $stmt;
    }
    
    public function readActive() {
        $query = "SELECT t.*, u.first_name, u.last_name, ta.title as task_title
                  FROM " . $this->table_name . " t 
                  LEFT JOIN users u ON t.user_id = u.id 
                  LEFT JOIN tasks ta ON t.task_id = ta.id 
                  WHERE t.end_time IS NULL 
                  ORDER BY t.created_at DESC";
        
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        
        return $stmt;
    }
    
    public function readByTask($task_id) {
        $query = "SELECT t.*, u.first_name, u.last_name 
                  FROM " . $this->table_name . " t 
                  LEFT JOIN users u ON t.user_id = u.id 
                  LEFT JOIN tasks ta ON t.task_id = ta.id 
                  WHERE t.task_id = ? 
                  ORDER BY t.created_at DESC";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $task_id);
        $stmt->execute();
        
        return $stmt;
    }
    
    public function readOne() {
        $query = "SELECT t.*, u.first_name, u.last_name, ta.title as task_title
                  FROM " . $this->table_name . " t 
                  LEFT JOIN users u ON t.user_id = u.id 
                  LEFT JOIN tasks ta ON t.task_id = ta.id 
                  WHERE t.id = ? LIMIT 0,1";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        $stmt->execute();
        
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        
        $this->task_id = $row['task_id'];
        $this->user_id = $row['user_id'];
        $this->start_time = $row['start_time'];
        $this->end_time = $row['end_time'];
        $this->duration = $row['duration'];
        $this->description = $row['description'];
        $this->created_at = $row['created_at'];
        $this->updated_at = $row['updated_at'];
    }
    
    public function update() {
        $query = "UPDATE " . $this->table_name . " 
                  SET task_id=:task_id, user_id=:user_id, start_time=:start_time, 
                      end_time=:end_time, duration=:duration, description=:description, 
                      updated_at=:updated_at 
                  WHERE id=:id";
        
        $stmt = $this->conn->prepare($query);
        
        $this->task_id = htmlspecialchars(strip_tags($this->task_id));
        $this->user_id = htmlspecialchars(strip_tags($this->user_id));
        $this->start_time = htmlspecialchars(strip_tags($this->start_time));
        $this->end_time = htmlspecialchars(strip_tags($this->end_time));
        $this->duration = htmlspecialchars(strip_tags($this->duration));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->updated_at = gmdate('Y-m-d H:i:s');
        
        $stmt->bindParam(":task_id", $this->task_id);
        $stmt->bindParam(":user_id", $this->user_id);
        $stmt->bindParam(":start_time", $this->start_time);
        $stmt->bindParam(":end_time", $this->end_time);
        $stmt->bindParam(":duration", $this->duration);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":updated_at", $this->updated_at);
        $stmt->bindParam(":id", $this->id);
        
        if($stmt->execute()) {
            return true;
        }
        
        return false;
    }
    
    public function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        
        if($stmt->execute()) {
            return true;
        }
        
        return false;
    }
    
    public function stop($id) {
        $this->end_time = gmdate('Y-m-d H:i:s');
        $this->duration = $this->calculateDuration($this->start_time, $this->end_time);
        
        $query = "UPDATE " . $this->table_name . " 
                  SET end_time=:end_time, duration=:duration, updated_at=:updated_at 
                  WHERE id=:id";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(":end_time", $this->end_time);
        $stmt->bindParam(":duration", $this->duration);
        $stmt->bindParam(":updated_at", gmdate('Y-m-d H:i:s'));
        $stmt->bindParam(":id", $id);
        
        if($stmt->execute()) {
            return true;
        }
        
        return false;
    }
    
    private function calculateDuration($start_time, $end_time) {
        $start = new DateTime($start_time);
        $end = new DateTime($end_time);
        $interval = $start->diff($end);
        
        $hours = $interval->format('%H');
        $minutes = $interval->format('%I');
        $seconds = $interval->format('%s');
        
        return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
    }
}
?>
