<?php

class functions
{
    public function user_did($message)
    {
        $db = new db();
        $db->query("INSERT INTO `user_log` (`user_id`, `log`) VALUES ('{$_SESSION['user_id']}', '{$message}')");
    }

    public function user_park_credits($user_id)
    {
        $db = new db();
        $res = $db->query("SELECT SUM(amount) as total FROM visit_trans WHERE user_id = {$user_id} AND type='PARK'");
        if ($res->num_rows < 1) {
            return 0;
        } else {
            return $res->fetch_assoc()['total'];
        }
    }

    public function user_training_credits($user_id)
    {
        $db = new db();
        $res = $db->query("SELECT SUM(amount) as total FROM visit_trans WHERE user_id = {$user_id} AND type='TRAINING'");
        if ($res->num_rows < 1) {
            return 0;
        } else {
            return $res->fetch_assoc()['total'];
        }
    }

    public function get_latest_booking($user_id)
    {
        $db = new db();
        $res = $db->query("SELECT * FROM bookings WHERE user_id = {$user_id} AND `status` = 1 ORDER BY record_id DESC LIMIT 1");
        if ($res->num_rows < 1) {
            return "NO BOOKINGS YET";
        } else {
            $data = $res->fetch_assoc();
            return $data['date_of_booking'] . " <br> " . $data['time_start'] . " - " . $data['time_end'];
        }
    }

    public function get_total_dogs($user_id)
    {
        $db = new db();
        $res = $db->query("SELECT * FROM user_animals WHERE user_id = {$user_id}");
        if ($res->num_rows < 1) {
            return "0";
        } else {
            return $res->num_rows;
        }
    }

    public function get_total_bookings()
    {
        $db = new db();
        $res = $db->query("SELECT * FROM bookings");
        if ($res->num_rows < 1) {
            return "0";
        } else {
            return $res->num_rows;
        }
    }

    public function get_total_bookings_last_month()
    {
        $db = new db();
        $date = new DateTime('first day of this month');
        $date->sub(new DateInterval('P1D'));
        $last_day_of_prev_month = $date->format('Y-m-d');
        $first_day_of_prev_month = $date->format('Y-m-01');

        $res = $db->query("SELECT * FROM bookings WHERE date_of_booking BETWEEN '{$first_day_of_prev_month}' AND '{$last_day_of_prev_month}'");
        if ($res->num_rows < 1) {
            return "0";
        } else {
            return $res->num_rows;
        }
    }


    public function get_total_value_from_payments_last_month()
    {
        $db = new db();
        $date = new DateTime('first day of this month');
        $date->sub(new DateInterval('P1D'));
        $last_day_of_prev_month = $date->format('Y-m-d');
        $first_day_of_prev_month = $date->format('Y-m-01');

        $res = $db->query("SELECT SUM(zar) as total FROM payments WHERE `date_time` BETWEEN '{$first_day_of_prev_month}' AND '{$last_day_of_prev_month}'");
        if ($res->num_rows < 1) {
            return "0";
        } else {
            return $res->fetch_assoc()['total'];
        }
    }

    public function get_total_value_from_payments_this_month()
    {
        $db = new db();
        $date = new DateTime('first day of this month');
        $last_day_of_this_month = $date->format('Y-m-t');
        $first_day_of_this_month = $date->format('Y-m-01');

        $res = $db->query("SELECT SUM(zar) as total FROM payments WHERE `date_time` BETWEEN '{$first_day_of_this_month}' AND '{$last_day_of_this_month}'");
        if ($res->num_rows < 1) {
            return "0";
        } else {
            return $res->fetch_assoc()['total'];
        }
    }

    public function get_username($user_id){
        $db = new db();
        $res = $db->query("SELECT username FROM users WHERE record_id = {$user_id}");
        return $res->fetch_assoc()['username'];
    }

    public function get_user_email($user_id){
        $db = new db();
        $res = $db->query("SELECT email FROM users WHERE record_id = {$user_id}");
        return $res->fetch_assoc()['email'];
    }
}