describe("Batch Creation", () => {
	it("creates a new batch", () => {
		cy.login();
		cy.wait(500);
		cy.visit("/lms/batches");
		cy.closeOnboardingModal();

		// Open Settings
		cy.get("span").contains("Learning").click();
		cy.get("span").contains("Settings").click();

		// Add a new member
		cy.get("[data-dismissable-layer]")
			.find("span")
			.contains(/^Members$/)
			.click();
		cy.get("[data-dismissable-layer]")
			.find("button")
			.contains("New")
			.click();

		const dateNow = Date.now();
		const randomEmail = `testuser_${dateNow}@example.com`;
		const randomName = `Test User ${dateNow}`;

		cy.get("input[placeholder='jane@doe.com']").type(randomEmail);
		cy.get("input[placeholder='Jane']").type(randomName);
		cy.get("button").contains("Add").click();

		// Open Settings
		cy.get("span").contains("Learning").click();
		cy.get("span").contains("Settings").click();

		// Add evaluator
		cy.get("[data-dismissable-layer]")
			.find("span")
			.contains(/^Evaluators$/)
			.click();

		cy.get("[data-dismissable-layer]")
			.find("button")
			.contains("New")
			.click();
		const randomEvaluator = `evaluator${dateNow}@example.com`;

		cy.get("input[placeholder='jane@doe.com']").type(randomEvaluator);
		cy.get("button").contains("Add").click();
		cy.get("div").contains(randomEvaluator).should("be.visible").click();

		cy.visit("/lms/batches");
		cy.closeOnboardingModal();

		// Create a batch
		cy.get("button").contains("Create").click();
		cy.get("span").contains("New Batch").click();
		cy.wait(500);
		cy.get("label").contains("Title").type("Test Batch");
		cy.get("label").contains("Start Date").type("2030-10-01");
		cy.get("label").contains("End Date").type("2030-10-31");
		cy.get("label").contains("Start Time").type("10:00");
		cy.get("label").contains("End Time").type("11:00");
		cy.get("label").contains("Timezone").type("IST");
		cy.get("label").contains("Seat Count").type("10");

		cy.get("label")
			.contains("Description")
			.type("Test Batch Short Description to test the UI");
		cy.get("div[contenteditable=true").invoke(
			"text",
			"Test Batch Description. I need a very big description to test the UI. This is a very big description. It contains more than once sentence. Its meant to be this long as this is a UI test. Its unbearably long and I'm not sure why I'm typing this much. I'm just going to keep typing until I feel like its long enough. I think its long enough now. I'm going to stop typing now."
		);
		/* Instructor */
		cy.get("label")
			.contains("Instructors")
			.parent()
			.within(() => {
				cy.get("input").click().type("evaluator");
				cy.get("input")
					.invoke("attr", "aria-controls")
					.as("instructor_list_id");
			});
		cy.get("@instructor_list_id").then((instructor_list_id) => {
			cy.get(`[id^=${instructor_list_id}`)
				.should("be.visible")
				.within(() => {
					cy.get("[id^=headlessui-combobox-option-").first().click();
				});
		});
		cy.button("Save").click();
		cy.get("label").contains("Published").click();
		cy.button("Save").click();
		cy.wait(1000);
		let batchName;
		cy.url().then((url) => {
			console.log(url);
			batchName = url.split("/").pop().split("#")[0];
			cy.wrap(batchName).as("batchName");
		});
		cy.wait(500);

		// View Batch
		cy.wait(1000);
		cy.visit("/lms/batches");
		cy.closeOnboardingModal();

		cy.url().should("include", "/lms/batches");

		cy.get('[id^="headlessui-radiogroup-v-"]')
			.find("span")
			.contains("Upcoming")
			.should("be.visible")
			.click();

		cy.get("@batchName").then((batchName) => {
			cy.get(`a[href='/lms/batches/${batchName}'`).within(() => {
				cy.get("div").contains("Test Batch").should("be.visible");
				cy.get("div")
					.contains("Test Batch Short Description to test the UI")
					.should("be.visible");
				cy.get("span")
					.contains("01 Oct 2030 - 31 Oct 2030")
					.should("be.visible");
				cy.get("span")
					.contains("10:00 AM - 11:00 AM")
					.should("be.visible");
				cy.get("span").contains("IST").should("be.visible");
				cy.get("a").contains("Evaluator").should("be.visible");
				cy.contains("div:visible", "10 Seats Left").should(
					"be.visible"
				);
			});
			cy.get(`a[href='/lms/batches/${batchName}'`).click();
		});

		cy.get("div").contains("Test Batch").should("be.visible");
		cy.get("div")
			.contains("Test Batch Short Description to test the UI")
			.should("be.visible");
		cy.get("a").contains("Evaluator").should("be.visible");
		cy.get("span:visible")
			.contains("01 Oct 2030 - 31 Oct 2030")
			.should("be.visible");
		cy.get("span:visible")
			.contains("10:00 AM - 11:00 AM")
			.should("be.visible");
		cy.get("span:visible").contains("IST").should("be.visible");
		cy.contains("div:visible", "10 Seats Left").should("be.visible");

		cy.get("p")
			.contains(
				"Test Batch Description. I need a very big description to test the UI. This is a very big description. It contains more than once sentence. Its meant to be this long as this is a UI test. Its unbearably long and I'm not sure why I'm typing this much. I'm just going to keep typing until I feel like its long enough. I think its long enough now. I'm going to stop typing now."
			)
			.should("be.visible");
		cy.get("button:visible").contains("Dashboard").click();

		/* Add student to batch */
		cy.get("button").contains("Enroll").click();
		cy.get('div[role="dialog"]')
			.first()
			.find("div[label='Student']")
			.find("div")
			.first()
			.click();
		cy.get("input[placeholder='Search']").type(randomEmail);
		cy.get("div").contains(randomEmail).click();
		cy.get("button").contains("Submit").click();

		// Verify Seat Count
		cy.get("button:visible").contains("Overview").click();
		cy.contains("div:visible", "9 Seats Left").should("be.visible");
	});
});
