<?php

namespace App\Services;

use App\Helpers\SlaCalculator;
use App\Repositories\StatusTicketRepo;
use App\Repositories\TicketRepo;

class DashboardService
{

  private TicketRepo $tiketRepo;
  private StatusTicketRepo $statusTicketRepo;
  public function __construct()
  {
    $this->tiketRepo = new TicketRepo();
    $this->statusTicketRepo = new StatusTicketRepo();
  }

  public function getActiveTicket(
    ?string $periode
  ) {
    $periode = $this->getPeriode($periode);
    $tickets = $this->tiketRepo->findByDateRange($periode['startDate'], $periode['endDate'], true);

    return $tickets;
  }

  public function getSlaSummary(
    ?string $periode
  ): array {

    $periode = $this->getPeriode($periode);
    $activeSla = $this->getActiveSla($periode['startDate'], $periode['endDate']);

    return [
      "completedTicket" => $this->getCompletedSla($periode['startDate'], $periode['endDate']),
      "withinSla"        => $activeSla["withinSla"],
      "nearSla"          => $activeSla["nearSla"],
      "overSla"          => $activeSla["overSla"]
    ];
  }

  private function getCompletedSla(
    ?string $startDate,
    ?string $endDate
  ): array {

    $status = $this->statusTicketRepo->findByNamaStatus('SOLVED');
    $tickets = $this->tiketRepo->findCompletedSla($startDate, $endDate, $status[0]->getId());
    $totalCompleted = 0;
    $totalTicket = count($tickets);


    foreach ($tickets as $ticket) {
      $slaPercentage = SlaCalculator::getSlaCompleted($ticket['tanggal_dibuat'], $ticket['tanggal_selesai'], $ticket['waktu_penyelesaian_jam']);
      if ($slaPercentage > 15) {
        $totalCompleted += 1;
      }
    }
    return [
      "total" => $totalTicket,
      "completed" => $totalCompleted,
      "percentage" => round(($totalCompleted / $totalTicket) * 100),
      "remaining" => $totalTicket - $totalCompleted
    ];
  }

  private function getActiveSla(
    ?string $startDate,
    ?string $endDate
  ): array {

    $tickets = $this->tiketRepo->findOpenTicket($startDate, $endDate);
    $withinSla = 0;
    $nearSla = 0;
    $overSla = 0;

    foreach ($tickets as $ticket) {
      if ($ticket->getSlaId() > 0) {
        $slaPercentage = SlaCalculator::getSlaStatus($ticket->getTanggalDibuat(), $ticket->getDeadline())['percentage'];
        // 0 - 75 = dalam SLA
        // 75 - 95 = mendekati batas SLA
        // > 95 = melebihi batas SLA
        if ($slaPercentage <= 75) {
          $withinSla += 1;
        } else if ($slaPercentage > 75 && $slaPercentage <= 95) {
          $nearSla += 1;
        } else {
          $overSla += 1;
        }
      }
    }
    return [
      "withinSla" => $withinSla,
      "nearSla" => $nearSla,
      "overSla" => $overSla
    ];
  }

  private function getPeriode(string $periode)
  {

    $startDate = null;
    $endDate   = null;


    switch ($periode) {
      case 'today':
        $startDate = date('Y-m-d 00:00:00');
        $endDate   = date('Y-m-d 23:59:59');
        break;

      case 'week':
        $startDate = date('Y-m-d 00:00:00', strtotime('monday this week'));
        $endDate   = date('Y-m-d 23:59:59', strtotime('sunday this week'));
        break;

      case 'month':
        $startDate = date('Y-m-01 00:00:00');
        $endDate   = date('Y-m-t 23:59:59');
        break;

      case 'year':
        $startDate = date('Y-01-01 00:00:00');
        $endDate   = date('Y-12-31 23:59:59');
        break;

      default:
        $startDate = date('Y-m-01 00:00:00');
        $endDate   = date('Y-m-t 23:59:59');
        break;
    }

    return [
      'startDate' => $startDate,
      'endDate'   => $endDate
    ];
  }
}
