import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { AssetService } from "@/services/asset.service"
import { ItemService } from "@/services/item.service"
import { MovementService } from "@/services/movement.service"

export default async function ItemPage({
  params,
}: {
  params: Promise<{ itemId: string }>
}) {
  const { itemId } = await params
  const item = await ItemService.findByIdWithCategory(itemId)
  const assets = await AssetService.findByItemId(itemId)
  const movements = await MovementService.findAllByItemId(itemId)

  if (!item) {
    return <div>Item not found</div>
  }

  return (
    <div className="grid gap-6">
      <Card className="rounded-sm shadow-none">
        <CardHeader>
          <CardTitle>{item.name}</CardTitle>
        </CardHeader>
        <CardContent>
          <div className="grid grid-cols-2 gap-x-8 gap-y-2 text-sm">
            <div className="flex justify-between">
              <span className="text-gray-600">Category</span>
              <span>{item.category.name}</span>
            </div>
            <div className="flex justify-between">
              <span className="text-gray-600">Stock</span>
              <span>{item.stock}</span>
            </div>
          </div>
        </CardContent>
      </Card>

      {assets?.length > 0 && (
        <Card className="rounded-sm shadow-none">
          <CardHeader>
            <CardTitle>Assets</CardTitle>
          </CardHeader>
          <CardContent>
            {assets?.map((asset) => (
              <div
                key={asset.id}
                className="grid grid-cols-3 gap-x-8 gap-y-2 text-sm"
              >
                <div className="flex justify-between">
                  <span className="text-gray-600">Status</span>
                  <span>{asset.status || "Available"}</span>
                </div>
                <div className="flex justify-between">
                  <span className="text-gray-600">Serial Number</span>
                  <span>{asset.serialNumber}</span>
                </div>
                <div className="flex justify-between">
                  <span className="text-gray-600">Delivery Note</span>
                  <span>{asset.deliveryNote}</span>
                </div>
              </div>
            ))}
            {assets?.length === 0 && (
              <p className="col-span-2 text-center text-gray-500">
                No assets found.
              </p>
            )}
          </CardContent>
        </Card>
      )}

      {movements?.length > 0 && (
        <Card className="rounded-sm shadow-none">
          <CardHeader>
            <CardTitle>Movements</CardTitle>
          </CardHeader>
          <CardContent>
            {movements.map((movement) => (
              <div
                key={`${movement.id}-${movement.type}`}
                className="grid grid-cols-2 gap-x-8 gap-y-2 text-sm"
              >
                <div className="flex justify-between">
                  <span className="text-gray-600">Type</span>
                  <span>{movement.type}</span>
                </div>
                <div className="flex justify-between">
                  <span className="text-gray-600">Quantity</span>
                  <span>{movement.quantity}</span>
                </div>
              </div>
            ))}
          </CardContent>
        </Card>
      )}
    </div>
  )
}
