package tests

import "core:testing"
import "../src/core"
import "../src/shared"
import "../src/ui"

@test
ui_navigation_guard_blocks_locked_screen :: proc(t: ^testing.T) {
	state := core.new_initial_state()
	controller := ui.new_controller(state)
	defer ui.dispose_controller(&controller)

	err := ui.navigate_to_screen(&controller, .Layout)
	testing.expect(t, !shared.is_ok(err), "layout should be blocked before panels exist")
}

@test
ui_navigation_guard_allows_after_prereqs :: proc(t: ^testing.T) {
	state := core.new_initial_state()
	state.panel_images = make(map[string]core.Panel_Image)
	state.panel_images["p1"] = core.Panel_Image{url = "https://example.com/p1.png", width = 1, height = 1}

	controller := ui.new_controller(state)
	defer ui.dispose_controller(&controller)
	err := ui.navigate_to_screen(&controller, .Layout)
	testing.expect(t, shared.is_ok(err), "layout should be allowed once panel images exist")
}

@test
ui_background_job_lifecycle_and_cancel :: proc(t: ^testing.T) {
	state := core.new_initial_state()
	controller := ui.new_controller(state)
	defer ui.dispose_controller(&controller)

	job1 := ui.start_background_job(&controller, .Generate_Script, "job1")
	testing.expect(t, controller.state.workflow.is_generating, "generation should be on after starting a job")

	job2 := ui.start_background_job(&controller, .Generate_Panel, "job2")
	_ = ui.mark_job_running(&controller.jobs, job1)
	_ = ui.mark_job_running(&controller.jobs, job2)

	cerr := ui.cancel_background_job(&controller, job2)
	testing.expect(t, shared.is_ok(cerr), "cancel should succeed")

	_ = ui.finish_background_job(&controller, job1, "")
	testing.expect(t, !controller.state.workflow.is_generating, "generation should be off after active jobs are done")
}

@test
ui_workflow_transition_guard :: proc(t: ^testing.T) {
	state := core.new_initial_state()
	controller := ui.new_controller(state)
	defer ui.dispose_controller(&controller)

	err := ui.set_workflow_step(&controller, .Layout)
	testing.expect(t, !shared.is_ok(err), "should block invalid direct transition")

	err2 := ui.set_workflow_step(&controller, .Generating_Script)
	testing.expect(t, shared.is_ok(err2), "should allow valid transition to generating script")
}
