<?php

function dateWise_visitors($institution_id, $branch_id, $conn_rjllearning_specific, $start_date, $end_date)
{
    $result = [];
    $overall_total_visits = 0;
    $status_total = [
        'Pending' => 0,
        'Active' => 0,
        'Completed' => 0,
        'Denied' => 0
    ];

    function generateDailyDateRanges($start_date, $end_date)
    {
        $date_ranges = [];
        $current_date = new DateTime($start_date);
        $end_date = new DateTime($end_date);

        while ($current_date <= $end_date) {
            $date_ranges[] = [
                'start' => $current_date->format('Y-m-d'),
                'end' => $current_date->format('Y-m-d')
            ];
            $current_date->modify('+1 day');
        }

        return $date_ranges;
    }

    $date_ranges = generateDailyDateRanges($start_date, $end_date);

    $sql = "SELECT status, count(*) as visits 
                FROM visitors_log 
                WHERE institution_id = ? AND date >= ? AND date <= ?";

    if ($branch_id !== 'all') {
        $sql .= " AND branch_id = ?";
    }

    $sql .= " GROUP BY status";
    $stmt = $conn_rjllearning_specific->prepare($sql);

    foreach ($date_ranges as $temp) {
        $array_params = [$institution_id, $temp['start'], $temp['end']];
        if ($branch_id != 'all') {
            $array_params[] = $branch_id;
        }
        $stmt->execute($array_params);
        $daily_visitors = $stmt->fetchAll();

        $temp_date_data_array = [
            'date' => $temp['start'],
            'statuses' => [
                'Pending' => 0,
                'Active' => 0,
                'Completed' => 0,
                'Denied' => 0
            ],
            'total_visits' => 0
        ];
        foreach ($daily_visitors as $visitor) {
            $status = intval($visitor['status']);
            $count_visits = intval($visitor['visits']);

            $overall_total_visits += $count_visits;
            $temp_date_data_array['total_visits'] += $count_visits;

            if ($status == 0) {
                $temp_date_data_array['statuses']['Pending'] += $count_visits;
                $status_total['Pending'] += $count_visits;
            } else if ($status == 1) {
                $temp_date_data_array['statuses']['Active'] += $count_visits;
                $status_total['Active'] += $count_visits;
            } else if ($status == 2) {
                $temp_date_data_array['statuses']['Completed'] += $count_visits;
                $status_total['Completed'] += $count_visits;
            } else if ($status == -1) {
                $temp_date_data_array['statuses']['Denied'] += $count_visits;
                $status_total['Denied'] += $count_visits;
            }
        }

        $result[] = $temp_date_data_array;
    }

    return [
        'date_wise_info' => $result,
        'total_visitors' => $overall_total_visits,
        'stats_info' => $status_total
    ];
}

function fetchWeeklyVisitors($institution_id, $branch_id, $conn_rjllearning_specific, $start_date, $end_date)
{
    $result = [];
    $overall_total_visits = 0;
    $status_total = [
        'Pending' => 0,
        'Active' => 0,
        'Completed' => 0,
        'Denied' => 0
    ];


    function generateDateRanges($start_date, $end_date)
    {
        $date_ranges = [];
        $current_start_date = new DateTime($start_date);
        $end_date = new DateTime($end_date);

        while ($current_start_date <= $end_date) {
            $current_end_date = clone $current_start_date;
            $current_end_date->modify('+6 days');

            if ($current_end_date > $end_date) {
                $current_end_date = $end_date;
            }

            $date_ranges[] = [
                'start' => $current_start_date->format('Y-m-d'),
                'end' => $current_end_date->format('Y-m-d')
            ];

            // Move start date to the next week
            $current_start_date->modify('+7 days');
        }

        return $date_ranges;
    }

    // Example usage:
    $date_ranges = generateDateRanges($start_date, $end_date);


    $sql = "SELECT status, count(*) as visits 
        FROM visitors_log 
        WHERE institution_id = ? AND date >= ? AND date <= ?";


    if ($branch_id != 'all') {
        $sql .= " AND branch_id = ?";
    }

    $sql .= " GROUP BY status";
    $stmt = $conn_rjllearning_specific->prepare($sql);

    foreach ($date_ranges as $temp) {
        $array_params = [$institution_id, $temp['start'], $temp['end']];
        if ($branch_id != 'all') {
            $array_params[] = $branch_id;
        }
        $stmt->execute($array_params);
        $weekly_visitors = $stmt->fetchAll();


        $temp_date_data_array = array(
            'date' => $temp['start'] . ' - ' . $temp['end'],
            'statuses' => array(
                'Pending' => 0,
                'Active' => 0,
                'Completed' => 0,
                'Denied' => 0
            ),
            'total_visits' => 0
        );

        foreach ($weekly_visitors as $visitor) {
            $status = intval($visitor['status']);
            $count_visits = intval($visitor['visits']);

            $overall_total_visits += $count_visits;

            $temp_date_data_array['total_visits'] += $count_visits;

            if ($status == 0) {
                $temp_date_data_array['statuses']['Pending'] = $count_visits;
                $status_total['Pending'] += $count_visits;
            } else if ($status == 1) {
                $temp_date_data_array['statuses']['Active'] = $count_visits;
                $status_total['Active'] += $count_visits;
            } else if ($status == 2) {
                $temp_date_data_array['statuses']['Completed'] = $count_visits;
                $status_total['Completed'] += $count_visits;
            } else if ($status == -1) {
                $temp_date_data_array['statuses']['Denied'] = $count_visits;
                $status_total['Denied'] += $count_visits;
            }
        }


        $result[] = $temp_date_data_array;
    }


    return [
        'date_wise_info' => $result,
        'total_visitors' => $overall_total_visits,
        'stats_info' => $status_total
    ];
}

function fetchMonthlyVisitors($institution_id, $branch_id, $conn_rjllearning_specific, $start_date, $end_date)
{
    $result = [];
    $overall_total_visits = 0;
    $status_total = [
        'Pending' => 0,
        'Active' => 0,
        'Completed' => 0,
        'Denied' => 0,
    ];

    function generateMonthlyDateRanges($start_date, $end_date)
    {
        $date_ranges = [];
        $current_start_date = new DateTime($start_date);
        $end_date = new DateTime($end_date);

        while ($current_start_date <= $end_date) {
            // Calculate the end of the current month
            $current_end_date = clone $current_start_date;
            $current_end_date->modify('last day of this month');

            // If the end of the month is greater than the max end date, use the max end date
            if ($current_end_date > $end_date) {
                $current_end_date = $end_date;
            }

            // Add the date range to the array
            $date_ranges[] = [
                'start' => $current_start_date->format('Y-m-d'),
                'end' => $current_end_date->format('Y-m-d')
            ];

            // Move to the first day of the next month
            $current_start_date = clone $current_end_date;
            $current_start_date->modify('first day of next month');
        }

        return $date_ranges;
    }

    $date_ranges = generateMonthlyDateRanges($start_date, $end_date);

    $sql = "SELECT status, count(*) as visits 
        FROM visitors_log 
        WHERE institution_id = ? AND date >= ? AND date <= ?";


    if ($branch_id != 'all') {
        $sql .= " AND branch_id = ?";
    }

    $sql .= " GROUP BY status";
    $stmt = $conn_rjllearning_specific->prepare($sql);

    foreach ($date_ranges as $temp) {
        $array_params = [$institution_id, $temp['start'], $temp['end']];
        if ($branch_id != 'all') {
            $array_params[] = $branch_id;
        }
        $stmt->execute($array_params);
        $monthly_visitors = $stmt->fetchAll();

        $temp_date_data_array = array(
            'date' => $temp['start'] . ' - ' . $temp['end'],
            'statuses' => array(
                'Pending' => 0,
                'Active' => 0,
                'Completed' => 0,
                'Denied' => 0
            ),
            'total_visits' => 0
        );
        foreach ($monthly_visitors as $visitor) {
            $status = intval($visitor['status']);
            $count_visits = intval($visitor['visits']);

            $overall_total_visits += $count_visits;

            $temp_date_data_array['total_visits'] += $count_visits;

            if ($status == 0) {
                $temp_date_data_array['statuses']['Pending'] = $count_visits;
                $status_total['Pending'] += $count_visits;
            } else if ($status == 1) {
                $temp_date_data_array['statuses']['Active'] = $count_visits;
                $status_total['Active'] += $count_visits;
            } else if ($status == 2) {
                $temp_date_data_array['statuses']['Completed'] = $count_visits;
                $status_total['Completed'] += $count_visits;
            } else if ($status == -1) {
                $temp_date_data_array['statuses']['Denied'] = $count_visits;
                $status_total['Denied'] += $count_visits;
            }
        }


        $result[] = $temp_date_data_array;
    }
    return [
        'date_wise_info' => $result,
        'total_visitors' => $overall_total_visits,
        'stats_info' => $status_total
    ];
}
