// 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("AdminsClient", () => {
    test("identify", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = {
            type: "admin",
            id: "991267459",
            name: "Ciaran1 Lee",
            email: "admin1@email.com",
            job_title: "Associate",
            away_mode_enabled: false,
            away_mode_reassign: false,
            has_inbox_seat: true,
            team_ids: [814865],
            avatar: {
                type: "avatar",
                image_url: "https://static.intercomassets.com/assets/default-avatars/admins/128.png",
            },
            email_verified: true,
            app: {
                type: "app",
                id_code: "this_is_an_id1_that_should_be_at_least_40",
                name: "MyApp 1",
                region: "US",
                timezone: "America/Los_Angeles",
                created_at: 1734537243,
                identity_verification: false,
            },
        };
        server.mockEndpoint().get("/me").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.admins.identify();
        expect(response).toEqual({
            type: "admin",
            id: "991267459",
            name: "Ciaran1 Lee",
            email: "admin1@email.com",
            job_title: "Associate",
            away_mode_enabled: false,
            away_mode_reassign: false,
            has_inbox_seat: true,
            team_ids: [814865],
            avatar: {
                type: "avatar",
                image_url: "https://static.intercomassets.com/assets/default-avatars/admins/128.png",
            },
            email_verified: true,
            app: {
                type: "app",
                id_code: "this_is_an_id1_that_should_be_at_least_40",
                name: "MyApp 1",
                region: "US",
                timezone: "America/Los_Angeles",
                created_at: 1734537243,
                identity_verification: false,
            },
        });
    });

    test("away (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { away_mode_enabled: true, away_mode_reassign: true, away_status_reason_id: 12345 };
        const rawResponseBody = {
            type: "admin",
            id: "991267460",
            name: "Ciaran2 Lee",
            email: "admin2@email.com",
            job_title: "Associate",
            away_mode_enabled: true,
            away_mode_reassign: true,
            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] },
        };
        server
            .mockEndpoint()
            .put("/admins/1/away")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.admins.away({
            admin_id: 1,
            away_mode_enabled: true,
            away_mode_reassign: true,
            away_status_reason_id: 12345,
        });
        expect(response).toEqual({
            type: "admin",
            id: "991267460",
            name: "Ciaran2 Lee",
            email: "admin2@email.com",
            job_title: "Associate",
            away_mode_enabled: true,
            away_mode_reassign: true,
            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],
            },
        });
    });

    test("away (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { away_mode_enabled: true, away_mode_reassign: true };
        const rawResponseBody = {
            type: "admin",
            id: "991267460",
            name: "Ciaran2 Lee",
            email: "admin2@email.com",
            job_title: "Associate",
            away_mode_enabled: true,
            away_mode_reassign: true,
            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] },
        };
        server
            .mockEndpoint()
            .put("/admins/1/away")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.admins.away({
            admin_id: 1,
            away_mode_enabled: true,
            away_mode_reassign: true,
        });
        expect(response).toEqual({
            type: "admin",
            id: "991267460",
            name: "Ciaran2 Lee",
            email: "admin2@email.com",
            job_title: "Associate",
            away_mode_enabled: true,
            away_mode_reassign: true,
            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],
            },
        });
    });

    test("away (3)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { away_mode_enabled: true, away_mode_reassign: true };
        const rawResponseBody = {
            type: "admin",
            id: "991267460",
            name: "Ciaran2 Lee",
            email: "admin2@email.com",
            job_title: "Associate",
            away_mode_enabled: true,
            away_mode_reassign: true,
            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] },
        };
        server
            .mockEndpoint()
            .put("/admins/1/away")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.admins.away({
            admin_id: 1,
            away_mode_enabled: true,
            away_mode_reassign: true,
        });
        expect(response).toEqual({
            type: "admin",
            id: "991267460",
            name: "Ciaran2 Lee",
            email: "admin2@email.com",
            job_title: "Associate",
            away_mode_enabled: true,
            away_mode_reassign: true,
            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],
            },
        });
    });

    test("away (4)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { away_mode_enabled: true, away_mode_reassign: true };
        const rawResponseBody = { key: "value" };
        server
            .mockEndpoint()
            .put("/admins/1/away")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(400)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.admins.away({
                admin_id: 1,
                away_mode_enabled: true,
                away_mode_reassign: true,
            });
        }).rejects.toThrow(Intercom.BadRequestError);
    });

    test("away (5)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { away_mode_enabled: true, away_mode_reassign: true };
        const rawResponseBody = { type: "error.list", errors: [{ code: "code" }, { code: "code" }] };
        server
            .mockEndpoint()
            .put("/admins/1/away")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.admins.away({
                admin_id: 1,
                away_mode_enabled: true,
                away_mode_reassign: true,
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    test("away (6)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { away_mode_enabled: true, away_mode_reassign: true };
        const rawResponseBody = { key: "value" };
        server
            .mockEndpoint()
            .put("/admins/1/away")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.admins.away({
                admin_id: 1,
                away_mode_enabled: true,
                away_mode_reassign: true,
            });
        }).rejects.toThrow(Intercom.NotFoundError);
    });

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

        const rawResponseBody = {
            type: "activity_log.list",
            pages: {
                type: "pages",
                page: 1,
                next: { per_page: 2, starting_after: "your-cursor-from-response" },
                per_page: 20,
                total_pages: 1,
            },
            activity_logs: [
                {
                    id: "fca05814-4b72-4dce-ad4f-77a786a2c136",
                    performed_by: { type: "admin", id: "991267464", email: "admin5@email.com", ip: "127.0.0.1" },
                    metadata: {
                        sign_in_method: "email_password",
                        external_id: "f3b87a2e09d514c6c2e79b9a",
                        away_mode: true,
                        away_status_reason: "😌 On a break",
                        reassign_conversations: false,
                        source: "admin update from web - Admin id: 93",
                        update_by: 93,
                        update_by_name: "Joe Example",
                        conversation_assignment_limit: 15,
                        ticket_assignment_limit: 20,
                        team_assignment_limit: 50,
                        enabled: true,
                        consent_id: 149673,
                        expired_at: "2025-12-04T09:31:57Z",
                        before: { key: "value" },
                        after: { key: "value" },
                    },
                    created_at: 1734537253,
                    activity_type: "app_name_change",
                    activity_description: "Ciaran5 Lee changed your app name from before to after.",
                },
                {
                    id: "f48c653b-0185-48ac-a276-23d11006bafb",
                    performed_by: { type: "admin", id: "991267464", email: "admin5@email.com", ip: "127.0.0.1" },
                    metadata: {
                        sign_in_method: "email_password",
                        external_id: "f3b87a2e09d514c6c2e79b9a",
                        away_mode: true,
                        away_status_reason: "😌 On a break",
                        reassign_conversations: false,
                        source: "admin update from web - Admin id: 93",
                        update_by: 93,
                        update_by_name: "Joe Example",
                        conversation_assignment_limit: 15,
                        ticket_assignment_limit: 20,
                        team_assignment_limit: 50,
                        enabled: true,
                        consent_id: 149673,
                        expired_at: "2025-12-04T09:31:57Z",
                        before: { key: "value" },
                        after: { key: "value" },
                    },
                    created_at: 1734537253,
                    activity_type: "message_state_change",
                    activity_description:
                        "Ciaran5 Lee changed your Initial message title message from Initial message title to Eventual message title.",
                },
            ],
        };
        server
            .mockEndpoint()
            .get("/admins/activity_logs")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.admins.listAllActivityLogs({
            created_at_after: "1677253093",
            created_at_before: "1677861493",
        });
        expect(response).toEqual({
            type: "activity_log.list",
            pages: {
                type: "pages",
                page: 1,
                next: {
                    per_page: 2,
                    starting_after: "your-cursor-from-response",
                },
                per_page: 20,
                total_pages: 1,
            },
            activity_logs: [
                {
                    id: "fca05814-4b72-4dce-ad4f-77a786a2c136",
                    performed_by: {
                        type: "admin",
                        id: "991267464",
                        email: "admin5@email.com",
                        ip: "127.0.0.1",
                    },
                    metadata: {
                        sign_in_method: "email_password",
                        external_id: "f3b87a2e09d514c6c2e79b9a",
                        away_mode: true,
                        away_status_reason: "\uD83D\uDE0C On a break",
                        reassign_conversations: false,
                        source: "admin update from web - Admin id: 93",
                        update_by: 93,
                        update_by_name: "Joe Example",
                        conversation_assignment_limit: 15,
                        ticket_assignment_limit: 20,
                        team_assignment_limit: 50,
                        enabled: true,
                        consent_id: 149673,
                        expired_at: "2025-12-04T09:31:57Z",
                        before: {
                            key: "value",
                        },
                        after: {
                            key: "value",
                        },
                    },
                    created_at: 1734537253,
                    activity_type: "app_name_change",
                    activity_description: "Ciaran5 Lee changed your app name from before to after.",
                },
                {
                    id: "f48c653b-0185-48ac-a276-23d11006bafb",
                    performed_by: {
                        type: "admin",
                        id: "991267464",
                        email: "admin5@email.com",
                        ip: "127.0.0.1",
                    },
                    metadata: {
                        sign_in_method: "email_password",
                        external_id: "f3b87a2e09d514c6c2e79b9a",
                        away_mode: true,
                        away_status_reason: "\uD83D\uDE0C On a break",
                        reassign_conversations: false,
                        source: "admin update from web - Admin id: 93",
                        update_by: 93,
                        update_by_name: "Joe Example",
                        conversation_assignment_limit: 15,
                        ticket_assignment_limit: 20,
                        team_assignment_limit: 50,
                        enabled: true,
                        consent_id: 149673,
                        expired_at: "2025-12-04T09:31:57Z",
                        before: {
                            key: "value",
                        },
                        after: {
                            key: "value",
                        },
                    },
                    created_at: 1734537253,
                    activity_type: "message_state_change",
                    activity_description:
                        "Ciaran5 Lee changed your Initial message title message from Initial message title to Eventual message title.",
                },
            ],
        });
    });

    test("listAllActivityLogs (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("/admins/activity_logs")
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.admins.listAllActivityLogs({
                created_at_after: "created_at_after",
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    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: "admin.list",
            admins: [
                {
                    type: "admin",
                    id: "991267466",
                    name: "Ciaran7 Lee",
                    email: "admin7@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] },
                },
            ],
        };
        server.mockEndpoint().get("/admins").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.admins.list();
        expect(response).toEqual({
            type: "admin.list",
            admins: [
                {
                    type: "admin",
                    id: "991267466",
                    name: "Ciaran7 Lee",
                    email: "admin7@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],
                    },
                },
            ],
        });
    });

    test("list (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("/admins").respondWith().statusCode(401).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.admins.list();
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    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: "admin",
            id: "991267468",
            name: "Ciaran9 Lee",
            email: "admin9@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] },
        };
        server.mockEndpoint().get("/admins/1").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.admins.find({
            admin_id: 1,
        });
        expect(response).toEqual({
            type: "admin",
            id: "991267468",
            name: "Ciaran9 Lee",
            email: "admin9@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],
            },
        });
    });

    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("/admins/1").respondWith().statusCode(401).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.admins.find({
                admin_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("/admins/1").respondWith().statusCode(404).jsonBody(rawResponseBody).build();

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