<?php
class Product {
    private $db;
    
    public function __construct($db) {
        $this->db = $db;
    }
    
    // Create product
    public function create($userId, $data) {
        try {
            $data['user_id'] = $userId;
            // Set is_active to 1 (active) by default if not provided
            if (!isset($data['is_active'])) {
                $data['is_active'] = 1;
            }
            $productId = $this->db->insert('products', $data);
            if ($productId) {
                return ['success' => true, 'product_id' => $productId];
            }
            return ['success' => false, 'message' => 'Gagal menyimpan produk ke database'];
        } catch (Exception $e) {
            return ['success' => false, 'message' => $e->getMessage()];
        }
    }
    
    // Get product
    public function get($productId, $userId) {
        return $this->db->selectOne(
            "SELECT * FROM products WHERE id = ? AND user_id = ?",
            [$productId, $userId]
        );
    }
    
    // Get all products
    public function getAll($userId, $limit = 20, $offset = 0) {
        $limit = intval($limit);
        $offset = intval($offset);
        return $this->db->select(
            "SELECT * FROM products WHERE user_id = ? AND is_active = 1
             ORDER BY name ASC LIMIT $limit OFFSET $offset",
            [$userId]
        );
    }
    
    // Search products
    public function search($userId, $keyword) {
        return $this->db->select(
            "SELECT * FROM products WHERE user_id = ? AND is_active = 1 
             AND (name LIKE ? OR sku LIKE ? OR barcode LIKE ?) 
             LIMIT 30",
            [$userId, "%$keyword%", "%$keyword%", "%$keyword%"]
        );
    }
    
    // Get by SKU
    public function getBySku($userId, $sku) {
        return $this->db->selectOne(
            "SELECT * FROM products WHERE user_id = ? AND sku = ? AND is_active = 1",
            [$userId, $sku]
        );
    }
    
    // Get by barcode
    public function getByBarcode($userId, $barcode) {
        return $this->db->selectOne(
            "SELECT * FROM products WHERE user_id = ? AND barcode = ? AND is_active = 1",
            [$userId, $barcode]
        );
    }
    
    // Update product
    public function update($productId, $userId, $data) {
        try {
            return $this->db->update('products', $data, [
                'id' => $productId,
                'user_id' => $userId
            ]);
        } catch (Exception $e) {
            return false;
        }
    }
    
    // Delete product
    public function delete($productId, $userId) {
        try {
            return $this->db->delete('products', [
                'id' => $productId,
                'user_id' => $userId
            ]);
        } catch (Exception $e) {
            return false;
        }
    }
    
    // Get low stock products
    public function getLowStock($userId, $threshold = 10) {
        return $this->db->select(
            "SELECT * FROM products WHERE user_id = ? AND quantity <= ? AND is_active = 1 
             ORDER BY quantity ASC",
            [$userId, $threshold]
        );
    }
    
    // Get products by category
    public function getByCategory($userId, $category) {
        return $this->db->select(
            "SELECT * FROM products WHERE user_id = ? AND category = ? AND is_active = 1 
             ORDER BY name ASC",
            [$userId, $category]
        );
    }
    
    // Get product count
    public function getCount($userId) {
        return $this->db->count('products', ['user_id' => $userId]);
    }
    
    // Get total inventory value
    public function getTotalInventoryValue($userId) {
        $result = $this->db->select(
            "SELECT SUM(cost_price * quantity) as total_cost 
             FROM products WHERE user_id = ? AND is_active = 1",
            [$userId]
        );
        
        return $result[0]['total_cost'] ?? 0;
    }
}
