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

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("CreateContent Component From Expert Management (create)", () => {
    describe("Rendering", () => {
        it("close button Rendered", async () => {
            render(<MockAppWithProviders><CreateContent/></MockAppWithProviders>);
            await waitFor(() => {
                const cancelBtn = screen.queryByText('بستن');
                expect(cancelBtn).toBeInTheDocument();
            });
        });
        it("confirm button Rendered", async () => {
            render(<MockAppWithProviders><CreateContent/></MockAppWithProviders>);
            await waitFor(() => {
                const cancelBtn = screen.queryByText('ثبت');
                expect(cancelBtn).toBeInTheDocument();
            });
        });
    });
    describe("Form Submission", () => {
        it('Should enable the submit button when the inputs are valid', async () => {
            render(<MockAppWithProviders><CreateContent/></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 passwordInput = screen.queryByLabelText('رمز عبور');
            const positionInput = screen.queryByLabelText('سمت');

            selectDropdownItem(screen, "option-opener-province", "آذربایجان شرقی");
            selectDropdownItem(screen, "option-opener-role", "ادمین");
            selectDropdownItem(screen, "option-opener-gender", "مرد");

            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, passwordInput, 'passwordTest');
            setInputValue(screen, positionInput, 'positionTest');

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