import {FastifyPluginAsyncTypebox} from '@fastify/type-provider-typebox'
import {Type} from '@sinclair/typebox'
import {TradeDirection} from '../../../generated/ManagingApiTypes'

const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
  // Define route to open a position
  fastify.post('/open-position', {
    schema: {
      body: Type.Object({
        account: Type.String(),
        tradeType: Type.String(),
        ticker: Type.String(),
        direction: Type.String(),
        price: Type.Optional(Type.Number()),
        quantity: Type.Number(),
        leverage: Type.Number(),
        stopLossPrice: Type.Optional(Type.Number()),
        takeProfitPrice: Type.Optional(Type.Number())
      }),
      response: {
        200: Type.Object({
          success: Type.Boolean(),
          hash: Type.Optional(Type.String()),
          error: Type.Optional(Type.String())
        })
      }
    }
  }, async (request, reply) => {
    const { account, tradeType, ticker, direction, price, quantity, leverage, stopLossPrice, takeProfitPrice } = request.body
    
    // Call the openPosition method from the GMX plugin
    return request.openGmxPosition(
      reply,
      account,
      tradeType,
      ticker,
      direction as TradeDirection,
      price,
      quantity,
      leverage,
      stopLossPrice,
      takeProfitPrice
    )
  })

  // Define route to cancel orders
  fastify.post('/cancel-orders', {
    schema: {
      body: Type.Object({
        account: Type.String(),
        ticker: Type.String()
      }),
      response: {
        200: Type.Object({
          success: Type.Boolean(),
          error: Type.Optional(Type.String())
        })
      }
    }
  }, async (request, reply) => {
    const { account, ticker } = request.body
    
    // Call the cancelGmxOrders method from the GMX plugin
    return request.cancelGmxOrders(
      reply,
      account,
      ticker,
    )
  })

  // Define route to close a position
  fastify.post('/close-position', {
    schema: {
      body: Type.Object({
        account: Type.String(),
        ticker: Type.String(),
        direction: Type.String()
      }),
      response: {
        200: Type.Object({
          success: Type.Boolean(),
          hash: Type.Optional(Type.String()),
          error: Type.Optional(Type.String())
        })
      }
    }
  }, async (request, reply) => {
    const { account, ticker, direction } = request.body
    
    // Call the closePosition method from the GMX plugin
    return request.closeGmxPosition(
      reply,
      account,
      ticker,
      direction as TradeDirection
    )
  })

  // Define route to get a trade
  fastify.get('/trades', {
    schema: {
      querystring: Type.Object({
        account: Type.String(),
        ticker: Type.String()
      }),
      response: {
        200: Type.Object({
          success: Type.Boolean(),
          trades: Type.Optional(Type.Array(Type.Any())),
          error: Type.Optional(Type.String())
        })
      }
    }
  }, async (request, reply) => {
    const { account, ticker } = request.query
    
    // Call the getGmxTrade method from the GMX plugin
    const result = await request.getGmxTrade(
      reply,
      account,
      ticker
    )

    console.log('result', result)
    
    return result
  })

  // Define route to get positions
  fastify.get('/positions', {
    schema: {
      querystring: Type.Object({
        account: Type.String()
      }),
      response: {
        200: Type.Object({
          success: Type.Boolean(),
          positions: Type.Optional(Type.Array(Type.Any())), // Using Type.Any() for now
          error: Type.Optional(Type.String())
        })
      }
    }
  }, async (request, reply) => {
    const { account } = request.query
    
    // Call the getGmxPositions method from the GMX plugin
    return request.getGmxPositions(
      reply,
      account
    )
  })
}

export default plugin 