// 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("AttributesClient", () => {
    test("create (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            name: "Attribute Title",
            description: "Attribute Description",
            data_type: "string",
            required_to_create: false,
        };
        const rawResponseBody = {
            type: "ticket_type_attribute",
            id: "157",
            workspace_id: "this_is_an_id640_that_should_be_at_least_",
            name: "Attribute Title",
            description: "Attribute Description",
            data_type: "string",
            input_options: { multiline: false },
            order: 2,
            required_to_create: false,
            required_to_create_for_contacts: false,
            visible_on_create: true,
            visible_to_contacts: true,
            default: false,
            ticket_type_id: 63,
            archived: false,
            created_at: 1734537862,
            updated_at: 1734537862,
        };
        server
            .mockEndpoint()
            .post("/ticket_types/ticket_type_id/attributes")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.ticketTypes.attributes.create({
            ticket_type_id: "ticket_type_id",
            name: "Attribute Title",
            description: "Attribute Description",
            data_type: "string",
            required_to_create: false,
        });
        expect(response).toEqual({
            type: "ticket_type_attribute",
            id: "157",
            workspace_id: "this_is_an_id640_that_should_be_at_least_",
            name: "Attribute Title",
            description: "Attribute Description",
            data_type: "string",
            input_options: {
                multiline: false,
            },
            order: 2,
            required_to_create: false,
            required_to_create_for_contacts: false,
            visible_on_create: true,
            visible_to_contacts: true,
            default: false,
            ticket_type_id: 63,
            archived: false,
            created_at: 1734537862,
            updated_at: 1734537862,
        });
    });

    test("create (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { name: "name", description: "description", data_type: "string" };
        const rawResponseBody = { type: "error.list", errors: [{ code: "code" }, { code: "code" }] };
        server
            .mockEndpoint()
            .post("/ticket_types/ticket_type_id/attributes")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.ticketTypes.attributes.create({
                ticket_type_id: "ticket_type_id",
                name: "name",
                description: "description",
                data_type: "string",
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    test("update (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { description: "New Attribute Description" };
        const rawResponseBody = {
            type: "ticket_type_attribute",
            id: "162",
            workspace_id: "this_is_an_id644_that_should_be_at_least_",
            name: "name",
            description: "New Attribute Description",
            data_type: "string",
            input_options: { key: "value" },
            order: 0,
            required_to_create: false,
            required_to_create_for_contacts: false,
            visible_on_create: false,
            visible_to_contacts: false,
            default: false,
            ticket_type_id: 65,
            archived: false,
            created_at: 1734537864,
            updated_at: 1734537864,
        };
        server
            .mockEndpoint()
            .put("/ticket_types/ticket_type_id/attributes/attribute_id")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.ticketTypes.attributes.update({
            ticket_type_id: "ticket_type_id",
            attribute_id: "attribute_id",
            description: "New Attribute Description",
        });
        expect(response).toEqual({
            type: "ticket_type_attribute",
            id: "162",
            workspace_id: "this_is_an_id644_that_should_be_at_least_",
            name: "name",
            description: "New Attribute Description",
            data_type: "string",
            input_options: {
                key: "value",
            },
            order: 0,
            required_to_create: false,
            required_to_create_for_contacts: false,
            visible_on_create: false,
            visible_to_contacts: false,
            default: false,
            ticket_type_id: 65,
            archived: false,
            created_at: 1734537864,
            updated_at: 1734537864,
        });
    });

    test("update (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {};
        const rawResponseBody = { type: "error.list", errors: [{ code: "code" }, { code: "code" }] };
        server
            .mockEndpoint()
            .put("/ticket_types/ticket_type_id/attributes/attribute_id")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.ticketTypes.attributes.update({
                ticket_type_id: "ticket_type_id",
                attribute_id: "attribute_id",
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });
});
