'use client';

import { ArrowLeft } from 'lucide-react';
import { useRouter } from 'next/navigation';

import { Button } from '@/components/ui/button';
import { PERMISSIONS } from '@/constants/permissions';
import { PermissionGuard } from '@/guards';
import type { TicketDetail, TicketOverviewDetail } from '@/types';

import { AssignTicketAction } from './actions/AssignTicketAction';
import { OpenRepairReviewAction } from './actions/OpenRepairReviewAction';

export function TicketDetailHeader({
  ticket,
  classDetail,
}: {
  ticket: TicketOverviewDetail;
  classDetail?: TicketDetail;
}) {
  const router = useRouter();
  const ticketLabel = ticket.ticket_name || ticket.id;

  return (
    <div className="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
      <div className="min-w-0 space-y-1">
        <h1 className="text-2xl font-semibold tracking-tight">Ticket Detail</h1>
        <p className="text-xs ">Ticket: {ticketLabel}</p>
      </div>

      <div className="flex shrink-0 items-center gap-2">
        {classDetail?.assignment_status === 'under_review' ? (
          <PermissionGuard permissions={PERMISSIONS.TICKET.REVIEW}>
            <OpenRepairReviewAction ticket={classDetail} />
          </PermissionGuard>
        ) : null}
        <PermissionGuard permissions={PERMISSIONS.TICKET.ASSIGN}>
          <AssignTicketAction ticketId={ticket.id} />
        </PermissionGuard>
        <Button variant="secondary" onClick={() => router.push('/ticket')}>
          <ArrowLeft className="size-4" />
          Back to List
        </Button>
      </div>
    </div>
  );
}
