from __future__ import annotations
from pathlib import Path
from admin.services.util import append_log
class EntrypointError(Exception):0
def resolve_entrypoint(app_dir,hint,log_path):
	J='wsgi';I='.py';F='app';D=log_path;C=app_dir;B=hint;B=(B or'app:app').strip();append_log(D,f"Resolving entrypoint hint: {B}")
	if':'in B and not B.endswith(I):
		A,M,E=B.partition(':');K=C/f"{A.replace(".","/")}.py";L=C/A.replace('.','/')/'__init__.py'
		if K.is_file()or L.is_file()or(C/f"{A}.py").is_file():append_log(D,f"Using entrypoint {A}:{E}");return f"{A}:{E}"
		append_log(D,f"Entrypoint file not found for {A}; keeping {A}:{E}");return f"{A}:{E}"
	if B.endswith(I):
		A=Path(B).stem;G=C/B
		if not G.is_file():G=C/f"{A}.py"
		if not G.is_file():raise EntrypointError(f"Entrypoint file not found: {B}")
		append_log(D,f"Mapped {B} -> {A}:app");return f"{A}:app"
	if(C/f"{B}.py").is_file():append_log(D,f"Using {B}:app");return f"{B}:app"
	for(A,E)in((F,F),(J,'application'),(J,F),('main',F)):
		if(C/f"{A}.py").is_file():H=f"{A}:{E}";append_log(D,f"Auto-detected entrypoint {H}");return H
	raise EntrypointError('Could not resolve entrypoint. Provide app:app or path to app.py')
