import {fireEvent, render, screen, waitFor} from "@testing-library/react";
import React from 'react';
import MockAppWithProviders from "../../../../../../../../mocks/AppWithProvider";
import UpdateContent from "../../UpdateContent";

const row = {
    original: {
        avatar: null,
        created_at: "2023-10-10T13:05:01.000000Z",
        email: "fisher.sigrid@yahoo.com",
        full_name: "Anibal Labadie",
        gender: "female",
        id: 85,
        national_id: "5944646138",
        phone_number: "09731311720",
        position: "boss1",
        province_fa: "اردبیل",
        province_id: 3,
        roles: [{
            created_at: "2023-10-10T13:03:18.000000Z",
            guard_name: "api",
            id: 2,
            name: "manager",
            name_fa: "مدیر"
        }],
        telephone_id: "tel-3110",
        updated_at: "2023-10-17T10:57:12.000000Z",
        username: "florence.kihn",
    }
}

function selectDropdownItem(screen, openerTestId, itemText) {
    const opener = screen.getByTestId(openerTestId);
    fireEvent.mouseDown(opener);
    const selectItem = screen.queryByText(itemText);
    fireEvent.click(selectItem);
}

function setInputValue(screen, inputElement, value) {
    fireEvent.change(inputElement, {target: {value}});
}

describe("UpdateContent Component From Expert Management (update)", () => {
    describe("Rendering", () => {
        it("close button Rendered", async () => {
            render(<MockAppWithProviders><UpdateContent row={row}/></MockAppWithProviders>);
            await waitFor(() => {
                const cancelBtn = screen.queryByText('بستن');
                expect(cancelBtn).toBeInTheDocument();
            });
        });
        it("confirm button Rendered", async () => {
            render(<MockAppWithProviders><UpdateContent row={row}/></MockAppWithProviders>);
            await waitFor(() => {
                const confirmBtn = screen.queryByText('ثبت');
                expect(confirmBtn).toBeInTheDocument();
            });
        });
    });
    describe("Form Submission", () => {
        it('Should enable the submit button when the inputs are valid', async () => {
            render(<MockAppWithProviders><UpdateContent row={row}/></MockAppWithProviders>);

            const submitButton = screen.queryByText('ثبت');

            const nameInput = screen.queryByLabelText('نام کامل');
            const usernameInput = screen.queryByLabelText('نام کاربری');
            const emailInput = screen.queryByLabelText('پست الکترونیک');
            const phoneNumberInput = screen.queryByLabelText('شماره همراه');
            const telephoneIdInput = screen.queryByLabelText('کد تلفن');
            const nationalIdInput = screen.queryByLabelText('کد ملی');
            const positionInput = screen.queryByLabelText('سمت');

            setInputValue(screen, nameInput, 'nameTest');
            setInputValue(screen, usernameInput, 'usernameTest');
            setInputValue(screen, emailInput, 'emailTest');
            setInputValue(screen, phoneNumberInput, 'phoneNumberTest');
            setInputValue(screen, telephoneIdInput, 'telephoneIdTest');
            setInputValue(screen, nationalIdInput, 'nationalIdTest');
            setInputValue(screen, positionInput, 'positionTest');

            await waitFor(() => {
                expect(submitButton).not.toBeDisabled();
            });
        });
    })
});