import { rest, RestRequest } from 'msw';
import {
  mockedCheckSmsKeyReturnData,
  mockedIntroducer,
  mockedLoginReturnData,
  mockedMobile,
  mockedPassword,
  mockedRefreshReturnData,
  mockedRefreshToken,
  mockedRestrictedMobile,
  mockedSettleRequest,
  mockedSettleRequests,
  mockedSmsKey,
  mockedTempUserData,
  mockedUser
} from './data';
import { FetchFreshTokenDataType, FetchTokenDataType } from '../api/auth';
import { CheckSmsKeyRequestDataType, SendSmsKeyRequestDataType } from '../api/temp-user';

export const api = (path: string) => new URL(path, process.env.REACT_APP_API_BASE_URL).toString();

export const handlers = [
  rest.post(api('/api/auth/login'), (req: RestRequest<FetchTokenDataType>, res, ctx) => {
    const { mobile, password } = req.body;
    if (mobile === mockedMobile && password === mockedPassword)
      return res(ctx.json(mockedLoginReturnData));
    return res(ctx.status(400), ctx.json({ message: 'Parse Errror' }));
  }),
  rest.post(api('/api/auth/refresh'), (req: RestRequest<FetchFreshTokenDataType>, res, ctx) => {
    const { refresh } = req.body;
    if (refresh === mockedRefreshToken) return res(ctx.json(mockedRefreshReturnData));
    return res(ctx.status(400), ctx.json({ message: 'Parse Errror' }));
  }),
  rest.get(api('/api/users/user'), (req, res, ctx) => {
    return res(ctx.json(mockedUser));
  }),
  rest.post(api('/api/users/users'), (req, res, ctx) => {
    return res(ctx.json(mockedUser));
  }),
  rest.post(api('/api/users/temp-users'), (req, res, ctx) => {
    return res(ctx.json(mockedTempUserData));
  }),
  rest.patch(
    api('/api/users/temp-users/send-sms-key'),
    (req: RestRequest<SendSmsKeyRequestDataType>, res, ctx) => {
      const { mobile } = req.body;
      if (mobile === mockedRestrictedMobile)
        return res(
          ctx.status(403),
          ctx.json('محدودیت‌ ارسال کد پیامکی برای شما به پایان رسیده است.')
        );
      return res(ctx.json(mockedTempUserData));
    }
  ),
  rest.patch(
    api('/api/users/temp-users/check-sms-key'),
    (req: RestRequest<CheckSmsKeyRequestDataType>, res, ctx) => {
      const { smsKey, mobile } = req.body;
      if (smsKey === mockedSmsKey && mobile === mockedMobile)
        return res(ctx.json(mockedCheckSmsKeyReturnData));
      if (mobile === mockedRestrictedMobile)
        return res(
          ctx.status(403),
          ctx.json('محدودیت‌ وارد کردن کد پیامکی اشتباه برای شما به پایان رسیده است.')
        );
      return res(ctx.status(400), ctx.json({ smsKey: 'کد وارد شده اشتباه است.' }));
    }
  ),
  rest.post(api('/api/introducers/introducers'), (req, res, ctx) => {
    return res(ctx.json(mockedIntroducer));
  }),
  rest.get(api('/api/introducers/introducer'), (req, res, ctx) => {
    return res(ctx.json(mockedIntroducer));
  }),
  rest.get(api('/api/introducers/settle-requests'), (req, res, ctx) => {
    return res(ctx.json(mockedSettleRequests));
  }),
  rest.post(api('/api/introducers/settle-requests'), (req, res, ctx) => {
    return res(ctx.json(mockedSettleRequest));
  })
];
