import { StatusBadge } from "@/components/ui/badge";
import type { Driver } from "@/types/driver";
import type { OrderStatus } from "@/types/order";
import type { VehicleDocumentState, VehicleTypeValue } from "@/types/dashboard";

type Variant = "default" | "primary" | "secondary" | "outline" | "success" | "warning" | "destructive" | "accent" | "info";

const ORDER_STATUS_VARIANT: Record<OrderStatus, Variant> = {
  pending: "warning",
  // An offer waiting for an answer, drawn like the pending it may become again — the order is
  // nobody's yet, and colouring it like a delivery under way says otherwise.
  assigned: "primary",
  accepted: "info",
  picked_up: "info",
  on_the_way: "info",
  delivered: "success",
  delivery_failed: "destructive",
  // Neutral, not destructive: somebody decided this. Colouring a cancellation like a failed
  // delivery sends an operations screen chasing a problem that does not exist.
  cancelled: "secondary",
};

export function OrderStatusBadge({
  status,
  label,
}: {
  status: OrderStatus;
  label: string;
}) {
  return (
    <StatusBadge variant={ORDER_STATUS_VARIANT[status]}>{label}</StatusBadge>
  );
}

const DRIVER_STATUS_VARIANT: Record<Driver["status"], Variant> = {
  pending: "warning",
  documents_required: "accent",
  approved: "success",
  rejected: "destructive",
  suspended: "secondary",
};

export function DriverStatusBadge({
  status,
  label,
  className,
}: {
  status: Driver["status"];
  label: string;
  className?: string;
}) {
  return (
    <StatusBadge variant={DRIVER_STATUS_VARIANT[status]} className={className}>
      {label}
    </StatusBadge>
  );
}

const EMPLOYMENT_VARIANT: Record<Driver["employment_type"], Variant> = {
  employee: "primary",
  freelance: "secondary",
};

export function EmploymentBadge({
  type,
  label,
}: {
  type: Driver["employment_type"];
  label: string;
}) {
  return <StatusBadge variant={EMPLOYMENT_VARIANT[type]}>{label}</StatusBadge>;
}

const OWNERSHIP_VARIANT = {
  company_owned: "primary",
  personal: "outline",
} as const;

export function OwnershipBadge({
  type,
  label,
}: {
  type: "company_owned" | "personal";
  label: string;
}) {
  return <StatusBadge variant={OWNERSHIP_VARIANT[type]}>{label}</StatusBadge>;
}

const DOCUMENT_STATUS_VARIANT: Record<VehicleDocumentState, Variant> = {
  missing: "secondary",
  expired: "destructive",
  expiring_soon: "warning",
  valid: "success",
};

export function DocumentStatusBadge({
  status,
  label,
}: {
  status: VehicleDocumentState;
  label: string;
}) {
  return <StatusBadge variant={DOCUMENT_STATUS_VARIANT[status]}>{label}</StatusBadge>;
}

const VEHICLE_TYPE_VARIANT: Record<VehicleTypeValue, Variant> = {
  motorcycle: "accent",
  car: "primary",
  van: "info",
  pickup_truck: "warning",
  truck: "secondary",
};

export function VehicleTypeBadge({
  type,
  label,
}: {
  type: VehicleTypeValue;
  label: string;
}) {
  return <StatusBadge variant={VEHICLE_TYPE_VARIANT[type]}>{label}</StatusBadge>;
}