import type { Id } from "../../../../../../convex/_generated/dataModel"
import { CreateLabelButton } from "../actions/CreateLabelButton"
import { AcceptReturnButton } from "../actions/AcceptReturnButton"
import { MarkReturnReceivedButton } from "../actions/MarkReturnReceivedButton"
import { IssueRefundButton } from "../actions/IssueRefundButton"

interface Props {
  orderId: Id<"orders">
  status: string
  paymentStatus: string
  trackingNumber?: string
  returnRequestedAt?: number
  returnReceivedAt?: number
  total: number
  currency: string
}

export function OrderActionsBar({
  orderId,
  status,
  paymentStatus,
  returnRequestedAt,
  returnReceivedAt,
  total,
  currency,
}: Props) {
  // confirmed + no return → create outbound label
  if (status === "confirmed" && !returnRequestedAt) {
    return (
      <div className="flex flex-wrap items-center gap-2">
        <CreateLabelButton orderId={orderId} />
      </div>
    )
  }

  // cancelled → refund if not already refunded
  if (status === "cancelled" && paymentStatus !== "refunded") {
    return (
      <div className="flex flex-wrap items-center gap-2">
        <IssueRefundButton orderId={orderId} total={total} currency={currency} />
      </div>
    )
  }

  // delivered + return requested → accept return (create return label)
  if (status === "delivered" && returnRequestedAt) {
    return (
      <div className="flex flex-wrap items-center gap-2">
        <AcceptReturnButton orderId={orderId} />
      </div>
    )
  }

  // processing + return in transit (accepted but not received) → mark received
  if (status === "processing" && returnRequestedAt && !returnReceivedAt) {
    return (
      <div className="flex flex-wrap items-center gap-2">
        <MarkReturnReceivedButton orderId={orderId} />
      </div>
    )
  }

  // completed → refund if not already refunded
  if (status === "completed" && paymentStatus !== "refunded") {
    return (
      <div className="flex flex-wrap items-center gap-2">
        <IssueRefundButton orderId={orderId} total={total} currency={currency} />
      </div>
    )
  }

  // All other states (pending, processing w/o return, shipped, delivered w/o return,
  // refunded, return status) → no actions
  return null
}
