<?php

namespace App\Services\Dashboard\Stat;

use App\Models\Order;
use App\Models\User;
use Carbon\Carbon;

class StatService
{
    private int $numberOfDays = 0;
    private $from;
    private $to;

    public function getLabels($from, $to)
    {
        $this->from = $from;
        $this->to = $to;

        // get the number of days between the two dates
        $this->numberOfDays = Carbon::parse($from)->diffInDays(Carbon::parse($to));

        // Start date
        $startDate = Carbon::parse($from);


        // End date
        $endDate = Carbon::parse($to);

        // Calculate the start date as 30 days ago
        // $startDate = Carbon::today()->subDays($numberOfDays - 1); // 29 because today is included

        // Initialize an array to hold the days
        $days = [];

        // Loop through each day from the start date to today
        for ($date = $startDate; $date->lte($endDate); $date->addDay()) {
            // Format each day like '21 August', '22 August', etc.
            $days[] = $date->day . ' ' . $date->translatedFormat('F');
        }

        return $days;
    }

    public function getStatics($type)
    {
        if ($type === 'orders') {
            return $this->getOrderStatics();
        } elseif ($type === 'users') {
            return $this->getUserStatics();
        } elseif ($type === 'sales') {
            return $this->getSalesStatics();
        }
    }

    private function getUserStatics(): array
    {
        // Step 1: Generate the last 10 days as an array
        $days = collect();
        for ($i = $this->numberOfDays - 1; $i >= 0; $i--) {
            $days->put(Carbon::parse($this->to)->subDays($i)->format('Y-m-d'), 0);
        }

        // Step 2: Get the user count for each day from the database
        $userRegistrations = User::where('created_at', '>=', Carbon::parse($this->to)->subDays($this->numberOfDays))
            ->selectRaw('DATE(created_at) as date, COUNT(*) as count')
            ->groupBy('date')
            ->orderBy('date', 'asc')
            ->pluck('count', 'date');

        // Step 3: Merge the user counts with the list of last 10 days, filling missing days with null
        $usersPerDay = $days->merge($userRegistrations)->toArray();

        return array_values($usersPerDay);
    }

    public function getOrderStatics(): array
    {
        // Step 1: Generate the last 10 days as an array
        $days = collect();
        for ($i = $this->numberOfDays - 1; $i >= 0; $i--) {
            $days->put(Carbon::parse($this->to)->subDays($i)->format('Y-m-d'), 0);
        }

        // Step 2: Get the user count for each day from the database
        $orders = Order::where('created_at', '>=', Carbon::parse($this->to)->subDays($this->numberOfDays))
            ->selectRaw('DATE(created_at) as date, COUNT(*) as count')
            ->groupBy('date')
            ->orderBy('date', 'asc')
            ->pluck('count', 'date');

        // Step 3: Merge the order counts with the list of last 10 days, filling missing days with null
        $ordersPerDay = $days->merge($orders)->toArray();

        return array_values($ordersPerDay);
    }

    public function getSalesStatics(): array
    {
        return [20];
    }
}
