import { test, expect } from '@playwright/test';

test.describe('Onboarding Flow', () => {
  test('should have a failed validation bad echo', async ({ page }) => {
    await page.route('*/**/api/echo/health', async (route) => {
      const json = { success: false };

      await route.fulfill({ json });
    });

    await page.goto('/');

    await page.getByTestId('edge-endpoint-url').clear();
    await page.getByTestId('edge-endpoint-url').fill('http://localhost:80809/api/echo');
    await page.waitForRequest('*/**/api/echo/health');

    const locator = await page.getByTestId('echo-endpoint-wrapper');
    await expect(locator).toHaveAttribute('data-error');

    const hasErrorAttribute = await locator.getAttribute('data-error');
    expect(hasErrorAttribute).toBe('true');
  });

  test('should have a succesfull validation for url', async ({ page }) => {
    await page.route('*/**/api/echo/health', async (route) => {
      const json = { success: true, health: 'ok' };

      await route.fulfill({ json });
    });

    await page.goto('/');

    await page.getByTestId('edge-endpoint-url').clear();
    await page.getByTestId('edge-endpoint-url').fill('http://localhost:80809/api/echo');
    await page.waitForRequest('*/**/api/echo/health');

    const locator = await page.getByTestId('echo-endpoint-wrapper');
    const hasErrorAttribute = await locator.getAttribute('data-error');
    expect(hasErrorAttribute).toBeNull();
  });

  test('should succesfully move to the next page', async ({ page }) => {
    await page.route('*/**/api/echo/health', async (route) => {
      const json = { success: true, health: 'ok' };

      await route.fulfill({ json });
    });

    await page.goto('/');

    await page.getByTestId('edge-endpoint-url').clear();
    await page.getByTestId('edge-endpoint-url').fill('http://localhost:80809/api/echo');
    await page.waitForRequest('*/**/api/echo/health');
    await page.route('*/**/api/workflows', async (route) => {
      await route.fulfill({ json: [] });
    });

    await page.getByRole('button', { name: 'Next step' }).click();

    const createWorkflowContent = await page.getByTestId('create-workflow-content-tab');
    expect(createWorkflowContent).toBeDefined();

    await page.route('*/**/api/workflows', async (route) => {
      const json = [
        {
          workflowId: 'workflow-id',
          options: {
            payloadSchema: {
              type: 'object',
              properties: {
                name: {
                  type: 'string',
                },
              },
            },
          },
          steps: [
            {
              stepId: 'send-email',
              type: 'email',
              inputs: {
                schema: {
                  type: 'object',
                  properties: {
                    showButton: {
                      type: 'boolean',
                      default: true,
                    },
                    username: {
                      type: 'string',
                      default: 'alanturing',
                    },
                    userImage: {
                      type: 'string',
                      default:
                        'https://react-email-demo-bdj5iju9r-resend.vercel.app/static/vercel-user.png',
                      format: 'uri',
                    },
                    invitedByUsername: {
                      type: 'string',
                      default: 'Alan',
                    },
                    invitedByEmail: {
                      type: 'string',
                      default: 'alan.turing@example.com',
                      format: 'email',
                    },
                    teamName: {
                      type: 'string',
                      default: 'Team Tomer',
                    },
                    teamImage: {
                      type: 'string',
                      default:
                        'https://react-email-demo-bdj5iju9r-resend.vercel.app/static/vercel-team.png',
                      format: 'uri',
                    },
                    inviteLink: {
                      type: 'string',
                      default: 'https://vercel.com/teams/invite/foo',
                      format: 'uri',
                    },
                    inviteFromIp: {
                      type: 'string',
                      default: '204.13.186.218',
                    },
                    inviteFromLocation: {
                      type: 'string',
                      default: 'São Paulo, Brazil',
                    },
                  },
                },
              },
              outputs: {
                schema: {
                  type: 'object',
                  properties: {
                    subject: {
                      type: 'string',
                    },
                    body: {
                      type: 'string',
                    },
                  },
                  required: ['subject', 'body'],
                  additionalProperties: false,
                },
              },
              results: {
                schema: {
                  type: 'object',
                  properties: {},
                  required: [],
                  additionalProperties: false,
                },
              },
              code: 'async (input)=>{\n        return {\n            subject: "Hello World " + payload.name,\n            body: (0,_react__WEBPACK_IMPORTED_MODULE_1__.renderEmail)(input)\n        };\n    }',
              options: {
                inputSchema: {
                  type: 'object',
                  properties: {
                    showButton: {
                      type: 'boolean',
                      default: true,
                    },
                    username: {
                      type: 'string',
                      default: 'alanturing',
                    },
                    userImage: {
                      type: 'string',
                      default:
                        'https://react-email-demo-bdj5iju9r-resend.vercel.app/static/vercel-user.png',
                      format: 'uri',
                    },
                    invitedByUsername: {
                      type: 'string',
                      default: 'Alan',
                    },
                    invitedByEmail: {
                      type: 'string',
                      default: 'alan.turing@example.com',
                      format: 'email',
                    },
                    teamName: {
                      type: 'string',
                      default: 'Team Tomer',
                    },
                    teamImage: {
                      type: 'string',
                      default:
                        'https://react-email-demo-bdj5iju9r-resend.vercel.app/static/vercel-team.png',
                      format: 'uri',
                    },
                    inviteLink: {
                      type: 'string',
                      default: 'https://vercel.com/teams/invite/foo',
                      format: 'uri',
                    },
                    inviteFromIp: {
                      type: 'string',
                      default: '204.13.186.218',
                    },
                    inviteFromLocation: {
                      type: 'string',
                      default: 'São Paulo, Brazil',
                    },
                  },
                },
              },
              providers: [],
            },
            {
              stepId: 'send-inapp',
              type: 'in_app',
              inputs: {
                schema: {
                  type: 'object',
                  properties: {},
                  required: [],
                  additionalProperties: false,
                },
              },
              outputs: {
                schema: {
                  type: 'object',
                  properties: {
                    body: {
                      type: 'string',
                    },
                  },
                  required: ['body'],
                  additionalProperties: false,
                },
              },
              results: {
                schema: {
                  type: 'object',
                  properties: {
                    seen: {
                      type: 'boolean',
                    },
                    read: {
                      type: 'boolean',
                    },
                    lastSeenDate: {
                      type: 'string',
                      format: 'date-time',
                      nullable: true,
                    },
                    lastReadDate: {
                      type: 'string',
                      format: 'date-time',
                      nullable: true,
                    },
                  },
                  required: ['seen', 'read', 'lastSeenDate', 'lastReadDate'],
                  additionalProperties: false,
                },
              },
              code: 'async (input)=>{\n        return {\n            body: "Hello In App"\n        };\n    }',
              options: {},
              providers: [],
            },
          ],
          code: 'async ({ step, payload })=>{\n    await step.email("send-email", async (input)=>{\n        return {\n            subject: "Hello World " + payload.name,\n            body: (0,_react__WEBPACK_IMPORTED_MODULE_1__.renderEmail)(input)\n        };\n    }, {\n        inputSchema: {\n            type: "object",\n            properties: {\n                showButton: {\n                    type: "boolean",\n                    default: true\n                },\n                username: {\n                    type: "string",\n                    default: "alanturing"\n                },\n                userImage: {\n                    type: "string",\n                    default: "https://react-email-demo-bdj5iju9r-resend.vercel.app/static/vercel-user.png",\n                    format: "uri"\n                },\n                invitedByUsername: {\n                    type: "string",\n                    default: "Alan"\n                },\n                invitedByEmail: {\n                    type: "string",\n                    default: "alan.turing@example.com",\n                    format: "email"\n                },\n                teamName: {\n                    type: "string",\n                    default: "Team Tomer"\n                },\n                teamImage: {\n                    type: "string",\n                    default: "https://react-email-demo-bdj5iju9r-resend.vercel.app/static/vercel-team.png",\n                    format: "uri"\n                },\n                inviteLink: {\n                    type: "string",\n                    default: "https://vercel.com/teams/invite/foo",\n                    format: "uri"\n                },\n                inviteFromIp: {\n                    type: "string",\n                    default: "204.13.186.218"\n                },\n                inviteFromLocation: {\n                    type: "string",\n                    default: "S\\xe3o Paulo, Brazil"\n                }\n            }\n        }\n    });\n    await step.inApp("send-inapp", async (input)=>{\n        return {\n            body: "Hello In App"\n        };\n    });\n}',
          data: {
            schema: {
              type: 'object',
              properties: {
                name: {
                  type: 'string',
                },
              },
            },
          },
          inputs: {
            schema: {
              type: 'object',
              properties: {},
              required: [],
              additionalProperties: false,
            },
          },
        },
      ];

      await route.fulfill({ json });
    });

    await page.waitForRequest('*/**/api/workflows');

    const textContent = await page
      .getByTestId('onboardin-workflow-link')
      .locator(page.getByTestId('onboarding-workflow-label'))
      .textContent();

    expect(textContent).toBe('workflow-id');
  });
});
