<?php

class Liquidation {

    private $_db,
            $_data,
            $_count = 0,
            $_errors = array();

    public function __construct() {
        $this->_db = DB::getInstance();
    }

    // CREATE LIQUIDATION
    public function insert($fields = array()) {
        if (!$this->_db->insert('liquidations', $fields)) {
            throw new Exception("There was a problem creating the liquidation.");
        }
        
        // Return the last inserted ID
        return $this->_db->lastInsertId();
    }

    // UPDATE LIQUIDATION
    public function update($fields = array(), $id = null) {
        if (!$this->_db->update('liquidations', $id, $fields)) {
            throw new Exception('There was a problem updating the liquidation.');
        }
    }

    // FIND LIQUIDATION
    public function find($liquidation_id = null) {
        if ($liquidation_id) {
            $data = $this->_db->query("SELECT * FROM `liquidations` WHERE `id` = ?", array($liquidation_id));
            if ($data->count()) {
                $this->_data = $data->first();
                return true;
            }
        }
        return false;
    }

    // GET ALL LIQUIDATIONS
    public function getAllLiquidations($filters = array()) {
        $sql = "SELECT * FROM liquidations WHERE 1=1";
        $params = array();

        // Apply filters
        if (isset($filters['user_id']) && !empty($filters['user_id'])) {
            $sql .= " AND user_id = ?";
            $params[] = $filters['user_id'];
        }

        if (isset($filters['status']) && !empty($filters['status'])) {
            $sql .= " AND status = ?";
            $params[] = $filters['status'];
        }

        // FIXED: Filter by created_date instead of liquidation_date for the filter
        if (isset($filters['from_date']) && !empty($filters['from_date'])) {
            $sql .= " AND created_date >= ?";
            $params[] = $filters['from_date'];
        }

        if (isset($filters['to_date']) && !empty($filters['to_date'])) {
            $sql .= " AND created_date <= ?";
            $params[] = $filters['to_date'];
        }

        $sql .= " ORDER BY created_date DESC, created_time DESC";

        $data = $this->_db->query($sql, $params);
        if ($data->count()) {
            $this->_count = $data->count();
            $this->_data = $data->results();
            return $this->_data;
        }
        return array();
    }

    // GET TOTAL LIQUIDATED AMOUNT FOR USER
    public function getTotalLiquidatedForUser($user_id, $status = 'paid') {
        $sql = "SELECT SUM(amount_liquidated) as total FROM liquidations WHERE user_id = ? AND status = ?";
        $query = $this->_db->query($sql, array($user_id, $status));
        
        if ($query->count()) {
            return (float) $query->first()->total;
        }
        return 0;
    }

    // GET PENDING LIQUIDATIONS FOR USER
    public function getPendingLiquidationsForUser($user_id) {
        $sql = "SELECT SUM(amount_liquidated) as total FROM liquidations WHERE user_id = ? AND status = 'pending'";
        $query = $this->_db->query($sql, array($user_id));
        
        if ($query->count()) {
            return (float) $query->first()->total;
        }
        return 0;
    }

    // DATA COLLECT
    public function data() {
        return $this->_data;
    }

    // COUNT
    public function count() {
        return $this->_count;
    }

    // ERRORS
    public function errors() {
        return $this->_errors;
    }
}

?>