<?php

namespace App\Controllers;

use App\Core\View;
use App\Helpers\SlaCalculator;
use App\Services\ActivityLogService;
use App\Services\AttachmentService;
use App\Services\CategoryService;
use App\Services\PriorityService;
use App\Services\SlaService;
use App\Services\StatusTicketService;
use App\Services\TeknisiService;
use App\Services\TicketAssignmentService;
use App\Services\TicketCommentService;
use App\Services\TicketService;
use App\Services\UserService;

class TeknisiController
{
  private TicketService $ticketService;
  private View $view;
  private CategoryService $categoryService;
  private PriorityService $prioritasService;
  private SlaService $slaService;
  private StatusTicketService $statusTicketService;
  private TeknisiService $teknisiService;
  private AttachmentService $attachmentService;
  private ActivityLogService $activityLogService;
  private SlaCalculator $slaCalculator;
  private UserService $userService;
  private TicketAssignmentService $ticketAssignmentService;
  private TicketCommentService $ticketCommentService;

  public function __construct()
  {
    $this->categoryService = new CategoryService();
    $this->prioritasService = new PriorityService();
    $this->slaService = new SlaService();
    $this->statusTicketService = new StatusTicketService();
    $this->teknisiService = new TeknisiService();
    $this->attachmentService = new AttachmentService();
    $this->activityLogService = new ActivityLogService();
    $this->slaCalculator = new SlaCalculator();
    $this->ticketService = new TicketService($this->activityLogService, $this->statusTicketService);
    $this->userService = new UserService();
    $this->ticketAssignmentService = new TicketAssignmentService();
    $this->ticketCommentService = new TicketCommentService();
    $this->view = new View();
  }

  public function index()
  {

    $user  = $_SESSION['user'];
    $tickets = $this->ticketService->findByTeknisiId($user['id']);

    $this->view->renderAdminPage(
      'ticket/index',
      [
        'tickets' => $tickets,
        'statusTiket' => $this->statusTicketService->findAll(),
        'daftarPrioritas' => $this->prioritasService->findAll(),
        'categories' => $this->categoryService->findAll(),
        'daftarSla' => $this->slaService->findAll()
      ]
    );
  }

  public function detail(int $id)
  {
    $ticket = $this->ticketService->findById($id);
    $currentStatus = $ticket->getNamaStatus();
    $availableStatus = $this->statusTicketService->getAvailableStatus($currentStatus);
    $daftarTeknisi = $this->teknisiService->findAktif();
    $attachments = $this->attachmentService->findByTicket($id);
    $timelines = $this->activityLogService->findByTicketId($id);
    $requester = $this->userService->findById($ticket->getUserId());
    $ticketAssignment = $this->ticketAssignmentService->findByTicketId($id);
    if ($ticketAssignment) {
      $teknisi = $this->teknisiService->findById($ticketAssignment->getTeknisiId());
      $teknisiUser = $this->userService->findById($teknisi->getUserId());
    }
    $comments = $this->ticketCommentService->findByTicketId($id);

    if (empty($ticket)) {
      $_SESSION['error'] = 'Tiket tidak ditemukan.';
      redirect('DAFTAR_TIKET');
      exit;
    }

    $slaResponse = $ticket->getSlaId() ? $this->slaCalculator->getSlaResponse($id) : null;
    $slaResolution = $ticket->getSlaId() ? $this->slaCalculator->getSlaResolution($id) : null;

    $this->view->renderAdminPage(
      'ticket/detail',
      [
        'ticket' => $ticket,
        'availableStatus' => $availableStatus,
        'daftarTeknisi' => $daftarTeknisi,
        'attachments' => $attachments,
        'timelines' => $timelines,
        'slaResponse' => $slaResponse,
        'slaResolution' => $slaResolution,
        'requester' => $requester,
        'teknisi' => $teknisiUser ?? null,
        'comments' => $comments
      ]
    );
  }
}
