<?php

namespace App\Controllers;

use App\Core\View;
use App\Helpers\SlaCalculator;
use App\Models\StatusTicketModel;
use App\Repositories\TicketRepo;
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 HomeController
{

  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;


  private TicketRepo $ticketRepo;


  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();
    $this->ticketRepo = new TicketRepo();
    $this->view = new View();
  }
  public function index(): void
  {
    $this->view->renderUserPage("user/home", [
      "title" => "Home"
    ]);
  }

  public function requestTicket()
  {
    $categories = $this->categoryService->findAll();
    $prioritas = $this->prioritasService->findAll();
    $slas = $this->slaService->findAll();
    $statusTickets = $this->statusTicketService->findAll();
    $statusOpen = array_filter(
      $statusTickets,
      fn(StatusTicketModel $status) => $status->getNamaStatus() === 'OPEN'
    );

    $statusOpen = reset($statusOpen);

    $this->view->renderUserPage(
      'ticket/tambah',
      [
        'categories' => $categories,
        'priorities' => $prioritas,
        'slas' => $slas,
        'statusOpen' => $statusOpen
      ]
    );
  }

  public function myActivity()
  {
    $user = $_SESSION['user'];
    $tickets = $this->ticketRepo->findByUser($user['id']);
    $this->view->renderUserPage("user/my-activity", [
      "title" => "My Activity",
      "tickets" => $tickets
    ]);
  }

  public function detailTicket(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->renderUserPage(
      'ticket/detail',
      [
        'ticket' => $ticket,
        'availableStatus' => $availableStatus,
        'daftarTeknisi' => $daftarTeknisi,
        'attachments' => $attachments,
        'timelines' => $timelines,
        'slaResponse' => $slaResponse,
        'slaResolution' => $slaResolution,
        'requester' => $requester,
        'teknisi' => $teknisiUser ?? null,
        'comments' => $comments
      ]
    );
  }
}
