// This file was auto-generated by Fern from our API Definition.

import * as Intercom from "../../src/api/index";
import { IntercomClient } from "../../src/Client";
import { mockServerPool } from "../mock-server/MockServerPool";

describe("NotesClient", () => {
    test("list (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = {
            type: "type",
            data: [
                {
                    type: "type",
                    id: "id",
                    created_at: 1,
                    contact: { type: "type", id: "id" },
                    author: {
                        type: "type",
                        id: "id",
                        name: "name",
                        email: "email",
                        job_title: "job_title",
                        away_mode_enabled: true,
                        away_mode_reassign: true,
                        away_status_reason_id: 1,
                        has_inbox_seat: true,
                        team_ids: [1, 1],
                        avatar: { image_url: "image_url" },
                        team_priority_level: { primary_team_ids: [1, 1], secondary_team_ids: [1, 1] },
                    },
                    body: "body",
                },
                {
                    type: "type",
                    id: "id",
                    created_at: 1,
                    contact: { type: "type", id: "id" },
                    author: {
                        type: "type",
                        id: "id",
                        name: "name",
                        email: "email",
                        job_title: "job_title",
                        away_mode_enabled: true,
                        away_mode_reassign: true,
                        away_status_reason_id: 1,
                        has_inbox_seat: true,
                        team_ids: [1, 1],
                        avatar: { image_url: "image_url" },
                        team_priority_level: { primary_team_ids: [1, 1], secondary_team_ids: [1, 1] },
                    },
                    body: "body",
                },
            ],
            total_count: 1,
        };
        server
            .mockEndpoint({ once: false })
            .get("/contacts/contact_id/notes")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const expected = {
            type: "type",
            data: [
                {
                    type: "type",
                    id: "id",
                    created_at: 1,
                    contact: {
                        type: "type",
                        id: "id",
                    },
                    author: {
                        type: "type",
                        id: "id",
                        name: "name",
                        email: "email",
                        job_title: "job_title",
                        away_mode_enabled: true,
                        away_mode_reassign: true,
                        away_status_reason_id: 1,
                        has_inbox_seat: true,
                        team_ids: [1, 1],
                        avatar: {
                            image_url: "image_url",
                        },
                        team_priority_level: {
                            primary_team_ids: [1, 1],
                            secondary_team_ids: [1, 1],
                        },
                    },
                    body: "body",
                },
                {
                    type: "type",
                    id: "id",
                    created_at: 1,
                    contact: {
                        type: "type",
                        id: "id",
                    },
                    author: {
                        type: "type",
                        id: "id",
                        name: "name",
                        email: "email",
                        job_title: "job_title",
                        away_mode_enabled: true,
                        away_mode_reassign: true,
                        away_status_reason_id: 1,
                        has_inbox_seat: true,
                        team_ids: [1, 1],
                        avatar: {
                            image_url: "image_url",
                        },
                        team_priority_level: {
                            primary_team_ids: [1, 1],
                            secondary_team_ids: [1, 1],
                        },
                    },
                    body: "body",
                },
            ],
            total_count: 1,
        };
        const page = await client.notes.list({
            contact_id: "contact_id",
        });

        expect(expected.data).toEqual(page.data);
        expect(page.hasNextPage()).toBe(true);
        const nextPage = await page.getNextPage();
        expect(expected.data).toEqual(nextPage.data);
    });

    test("list (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = { key: "value" };
        server
            .mockEndpoint({ once: false })
            .get("/contacts/contact_id/notes")
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.notes.list({
                contact_id: "contact_id",
            });
        }).rejects.toThrow(Intercom.NotFoundError);
    });

    test("create (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { body: "Hello", admin_id: "123" };
        const rawResponseBody = {
            type: "note",
            id: "31",
            created_at: 1734537390,
            contact: { type: "contact", id: "6762f0ad1bb69f9f2193bb62" },
            author: {
                type: "admin",
                id: "991267583",
                name: "Ciaran124 Lee",
                email: "admin124@email.com",
                job_title: "Associate",
                away_mode_enabled: false,
                away_mode_reassign: false,
                away_status_reason_id: 12345,
                has_inbox_seat: true,
                team_ids: [814865],
                avatar: { image_url: "https://example.org/128Wash.jpg" },
                team_priority_level: { primary_team_ids: [814865], secondary_team_ids: [493881] },
            },
            body: "<p>Hello</p>",
        };
        server
            .mockEndpoint()
            .post("/contacts/123/notes")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.notes.create({
            contact_id: "123",
            body: "Hello",
            admin_id: "123",
        });
        expect(response).toEqual({
            type: "note",
            id: "31",
            created_at: 1734537390,
            contact: {
                type: "contact",
                id: "6762f0ad1bb69f9f2193bb62",
            },
            author: {
                type: "admin",
                id: "991267583",
                name: "Ciaran124 Lee",
                email: "admin124@email.com",
                job_title: "Associate",
                away_mode_enabled: false,
                away_mode_reassign: false,
                away_status_reason_id: 12345,
                has_inbox_seat: true,
                team_ids: [814865],
                avatar: {
                    image_url: "https://example.org/128Wash.jpg",
                },
                team_priority_level: {
                    primary_team_ids: [814865],
                    secondary_team_ids: [493881],
                },
            },
            body: "<p>Hello</p>",
        });
    });

    test("create (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { body: "Hello", admin_id: "123" };
        const rawResponseBody = {
            type: "note",
            id: "31",
            created_at: 1734537390,
            contact: { type: "contact", id: "6762f0ad1bb69f9f2193bb62" },
            author: {
                type: "admin",
                id: "991267583",
                name: "Ciaran124 Lee",
                email: "admin124@email.com",
                job_title: "Associate",
                away_mode_enabled: false,
                away_mode_reassign: false,
                away_status_reason_id: 12345,
                has_inbox_seat: true,
                team_ids: [814865],
                avatar: { image_url: "https://example.org/128Wash.jpg" },
                team_priority_level: { primary_team_ids: [814865], secondary_team_ids: [493881] },
            },
            body: "<p>Hello</p>",
        };
        server
            .mockEndpoint()
            .post("/contacts/123/notes")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.notes.create({
            contact_id: "123",
            body: "Hello",
            admin_id: "123",
        });
        expect(response).toEqual({
            type: "note",
            id: "31",
            created_at: 1734537390,
            contact: {
                type: "contact",
                id: "6762f0ad1bb69f9f2193bb62",
            },
            author: {
                type: "admin",
                id: "991267583",
                name: "Ciaran124 Lee",
                email: "admin124@email.com",
                job_title: "Associate",
                away_mode_enabled: false,
                away_mode_reassign: false,
                away_status_reason_id: 12345,
                has_inbox_seat: true,
                team_ids: [814865],
                avatar: {
                    image_url: "https://example.org/128Wash.jpg",
                },
                team_priority_level: {
                    primary_team_ids: [814865],
                    secondary_team_ids: [493881],
                },
            },
            body: "<p>Hello</p>",
        });
    });

    test("create (3)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { body: "Hello", admin_id: "123" };
        const rawResponseBody = {
            type: "note",
            id: "31",
            created_at: 1734537390,
            contact: { type: "contact", id: "6762f0ad1bb69f9f2193bb62" },
            author: {
                type: "admin",
                id: "991267583",
                name: "Ciaran124 Lee",
                email: "admin124@email.com",
                job_title: "Associate",
                away_mode_enabled: false,
                away_mode_reassign: false,
                away_status_reason_id: 12345,
                has_inbox_seat: true,
                team_ids: [814865],
                avatar: { image_url: "https://example.org/128Wash.jpg" },
                team_priority_level: { primary_team_ids: [814865], secondary_team_ids: [493881] },
            },
            body: "<p>Hello</p>",
        };
        server
            .mockEndpoint()
            .post("/contacts/123/notes")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.notes.create({
            contact_id: "123",
            body: "Hello",
            admin_id: "123",
        });
        expect(response).toEqual({
            type: "note",
            id: "31",
            created_at: 1734537390,
            contact: {
                type: "contact",
                id: "6762f0ad1bb69f9f2193bb62",
            },
            author: {
                type: "admin",
                id: "991267583",
                name: "Ciaran124 Lee",
                email: "admin124@email.com",
                job_title: "Associate",
                away_mode_enabled: false,
                away_mode_reassign: false,
                away_status_reason_id: 12345,
                has_inbox_seat: true,
                team_ids: [814865],
                avatar: {
                    image_url: "https://example.org/128Wash.jpg",
                },
                team_priority_level: {
                    primary_team_ids: [814865],
                    secondary_team_ids: [493881],
                },
            },
            body: "<p>Hello</p>",
        });
    });

    test("create (4)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { body: "body" };
        const rawResponseBody = { key: "value" };
        server
            .mockEndpoint()
            .post("/contacts/contact_id/notes")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.notes.create({
                contact_id: "contact_id",
                body: "body",
            });
        }).rejects.toThrow(Intercom.NotFoundError);
    });

    test("find (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = {
            type: "note",
            id: "34",
            created_at: 1733846617,
            contact: { type: "contact", id: "6762f2591bb69f9f2193bc1f" },
            author: {
                type: "admin",
                id: "991267864",
                name: "Ciaran346 Lee",
                email: "admin346@email.com",
                job_title: "Associate",
                away_mode_enabled: false,
                away_mode_reassign: false,
                away_status_reason_id: 12345,
                has_inbox_seat: true,
                team_ids: [814865],
                avatar: { image_url: "https://example.org/128Wash.jpg" },
                team_priority_level: { primary_team_ids: [814865], secondary_team_ids: [493881] },
            },
            body: "<p>This is a note.</p>",
        };
        server.mockEndpoint().get("/notes/1").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.notes.find({
            note_id: 1,
        });
        expect(response).toEqual({
            type: "note",
            id: "34",
            created_at: 1733846617,
            contact: {
                type: "contact",
                id: "6762f2591bb69f9f2193bc1f",
            },
            author: {
                type: "admin",
                id: "991267864",
                name: "Ciaran346 Lee",
                email: "admin346@email.com",
                job_title: "Associate",
                away_mode_enabled: false,
                away_mode_reassign: false,
                away_status_reason_id: 12345,
                has_inbox_seat: true,
                team_ids: [814865],
                avatar: {
                    image_url: "https://example.org/128Wash.jpg",
                },
                team_priority_level: {
                    primary_team_ids: [814865],
                    secondary_team_ids: [493881],
                },
            },
            body: "<p>This is a note.</p>",
        });
    });

    test("find (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = { type: "error.list", errors: [{ code: "code" }, { code: "code" }] };
        server.mockEndpoint().get("/notes/1").respondWith().statusCode(401).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.notes.find({
                note_id: 1,
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    test("find (3)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = { key: "value" };
        server.mockEndpoint().get("/notes/1").respondWith().statusCode(404).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.notes.find({
                note_id: 1,
            });
        }).rejects.toThrow(Intercom.NotFoundError);
    });
});
