<?php

namespace App\Repositories\Core;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

trait DateStats
{
    public static function sumDaily($field, $nbDays = 15, $fieldDate = 'created_at')
    {
        $periods = DateRange::getPeriodsLastDay($nbDays);
        $values = self::sumPeriodly($periods, $field, $fieldDate);

        return self::buildLabels($periods, $values, 'dd Do MMM');
    }

    public static function countDaily($nbDays = 15, $fieldDate = 'created_at')
    {
        $periods = DateRange::getPeriodsLastDay($nbDays);
        $values = self::countPeriodly($periods, $fieldDate);

        return self::buildLabels($periods, $values, 'dd Do MMM');
    }

    public static function sumMonthly($field, $fieldDate = 'created_at')
    {
        $periods = DateRange::getPeriodsLastMonth(12);
        $values = self::sumPeriodly($periods, $field, $fieldDate);

        return self::buildLabels($periods, $values, 'MMM YY');
    }

    public static function countMonthly($fieldDate = 'created_at')
    {
        $periods = DateRange::getPeriodsLastMonth(12);
        $values = self::countPeriodly($periods, $fieldDate);

        return self::buildLabels($periods, $values, 'MMM YY');
    }

    public static function buildLabels($periods, $values, $format = 'MMM YY')
    {
        $data = [];
        foreach ($periods as $key => $period) {
            $index = Carbon::parse($period->startDate)->isoFormat($format);
            $data[$index] = $values[$key];
        }

        return $data;
    }

    public static function sumPeriodly($periods, $field, $fieldDate = 'created_at')
    {
        $data = [];
        foreach ($periods as $period) {
            $data[] = self::sumByPeriod($period->startDate, $period->endDate, $field, $fieldDate);
        }

        return $data;
    }

    public static function countPeriodly($periods, $fieldDate = 'created_at')
    {
        $data = [];
        foreach ($periods as $period) {
            $data[] = self::countByPeriod($period->startDate, $period->endDate, $fieldDate);
        }

        return $data;
    }

    public static function countByMonth($prev = 0, $field = 'created_at')
    {
        $start = Carbon::now()->subMonths($prev)->startOfMonth();
        $end = Carbon::now()->subMonths($prev)->endOfMonth();

        return self::countByPeriod($start, $end, $field);
    }

    public static function countByDay($prev = 0, $field = 'created_at')
    {
        $start = Carbon::now()->subDays($prev)->startOfMonth();
        $end = Carbon::now()->subDays($prev)->endOfMonth();

        return self::countByPeriod($start, $end, $field);
    }

    public static function countByPeriod($start, $end, $field = 'created_at')
    {
        return self::getModel()->whereBetween($field, [$start, $end])->count();
    }

    public static function avgByMonth($field, $prev = 0, $fieldDate = 'created_at')
    {
        $start = Carbon::now()->subMonths($prev)->startOfMonth();
        $end = Carbon::now()->subMonths($prev)->endOfMonth();

        return self::avgByPeriod($start, $end, $field, $fieldDate);
    }

    public static function avgByDay($field, $prev = 0, $fieldDate = 'created_at')
    {
        $start = Carbon::now()->subDays($prev)->startOfMonth();
        $end = Carbon::now()->subDays($prev)->endOfMonth();

        return self::avgByPeriod($start, $end, $field, $fieldDate);
    }

    public static function avgByPeriod($start, $end, $field, $fieldDate = 'created_at')
    {
        $c = self::countByPeriod($start, $end);

        return $c ? round(self::sumByPeriod($start, $end, $field, $fieldDate) / $c, 2) : 0;
    }

    public static function sumByMonth($field, $prev = 0, $fieldDate = 'created_at')
    {
        $start = Carbon::now()->subMonths($prev)->startOfMonth();
        $end = Carbon::now()->subMonths($prev)->endOfMonth();

        return self::sumByPeriod($start, $end, $field, $fieldDate);
    }

    public static function sumByDay($field, $prev = 0, $fieldDate = 'created_at')
    {
        $start = Carbon::now()->subDays($prev)->startOfDay();
        $end = Carbon::now()->subDays($prev)->endOfDay();

        return self::sumByPeriod($start, $end, $field, $fieldDate);
    }

    public static function sumByPeriod($start, $end, $field, $fieldDate = 'created_at')
    {
        return self::getModel()->whereBetween($fieldDate, [$start, $end])->sum($field);
    }

    public static function getModel()
    {
        return new Model();
    }
}
