import { IsBoolean, IsObject, IsOptional } from "class-validator"
import { EntityManager } from "typeorm"
import { defaultAdminOrdersRelations, defaultAdminOrdersFields } from "."
import { OrderService, SwapService } from "../../../../services"
import { validator } from "../../../../utils/validator"
/**
 * @oas [post] /orders/{id}/swaps/{swap_id}/fulfillments
 * operationId: "PostOrdersOrderSwapsSwapFulfillments"
 * summary: "Create a Swap Fulfillment"
 * description: "Creates a Fulfillment for a Swap."
 * x-authenticated: true
 * parameters:
 *   - (path) id=* {string} The id of the Order.
 *   - (path) swap_id=* {string} The id of the Swap.
 * requestBody:
 *   content:
 *     application/json:
 *       schema:
 *         properties:
 *           metadata:
 *             description: An optional set of key-value pairs to hold additional information.
 *             type: object
 *           no_notification:
 *             description: If set to true no notification will be send related to this Claim.
 *             type: boolean
 * tags:
 *   - Order
 * responses:
 *   200:
 *     description: OK
 *     content:
 *       application/json:
 *         schema:
 *           properties:
 *             order:
 *               $ref: "#/components/schemas/order"
 */
export default async (req, res) => {
  const { id, swap_id } = req.params

  const validated = await validator(
    AdminPostOrdersOrderSwapsSwapFulfillmentsReq,
    req.body
  )

  const orderService: OrderService = req.scope.resolve("orderService")
  const swapService: SwapService = req.scope.resolve("swapService")
  const entityManager: EntityManager = req.scope.resolve("manager")

  await entityManager.transaction(async (manager) => {
    await swapService.withTransaction(manager).createFulfillment(swap_id, {
      metadata: validated.metadata,
      no_notification: validated.no_notification,
    })

    const order = await orderService.withTransaction(manager).retrieve(id, {
      select: defaultAdminOrdersFields,
      relations: defaultAdminOrdersRelations,
    })

    res.status(200).json({ order })
  })
}

export class AdminPostOrdersOrderSwapsSwapFulfillmentsReq {
  @IsObject()
  @IsOptional()
  metadata?: object

  @IsBoolean()
  @IsOptional()
  no_notification?: boolean
}
