import { z } from 'zod'

import { HttpServer, type THttpServer } from '~/libs/http-server'

const userSchema = z.object({
  data: z.object({
    id: z.string(),
    email: z.string(),
    phone: z.string(),
    subscribe: z.object({
      id: z.string(),
      subscribe_plan_id: z.string(),
      start_date: z.string(),
      end_date: z.string(),
      status: z.string(),
      auto_renew: z.boolean(),
      subscribe_plan: z.object({
        id: z.string(),
        name: z.string(),
        code: z.string(),
      }),
    }),
  }),
})

export type TUser = z.infer<typeof userSchema>

export const getUser = async (parameters: THttpServer) => {
  try {
    const { data } = await HttpServer(parameters).get(`/api/user/profile`)
    return userSchema.parse(data)
  } catch (error) {
    // eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
    return Promise.reject(error)
  }
}
