from typing import Protocol

from datamodel_code_generator.parser._passes import helpers
from datamodel_code_generator.parser._passes._state import _FLOORS, _RUN_RECORD, _FinalizeState, _PipelineRun
from datamodel_code_generator.parser._pipeline import ForEachModule, PassSpec, Segment
from datamodel_code_generator.parser.base import ParserRunContext
from datamodel_code_generator.reference import ModelResolver


class PipelineHost(Protocol):
    use_annotated: bool
    model_resolver: ModelResolver
    run_context: ParserRunContext
    by_keyword: list[str]
    by_position: list[str]
    all_keywords: list[str]
    subscripted: list[str]
    in_a_module_loop: list[str]
    in_a_segment: list[str]
    in_a_function: list[str]
    in_a_run: list[str]


REGISTRY: dict[str, int] = {}
NAMES = ("use_annotated",)


def run(parser: PipelineHost, state: object) -> None:
    return None


def an_option(parser: PipelineHost, state: object) -> bool:
    return parser.use_annotated


def a_fact(parser: PipelineHost, state: object) -> bool:
    return bool(parser.in_a_function)


def view(state: _PipelineRun) -> _PipelineRun:
    return state


PIPELINE: tuple[PassSpec[PipelineHost, _PipelineRun], ...] = (
    PassSpec("option_by_keyword", run, when=lambda self, _: self.use_annotated),
    PassSpec("option_by_position", run, lambda self, _: self.use_annotated),
    PassSpec("fact_by_keyword", run, when=lambda self, _: bool(self.by_keyword)),
    PassSpec("fact_by_position", run, lambda self, _: bool(self.by_position)),
    PassSpec(name="fact_all_keywords", run=run, when=lambda self, _: len(self.all_keywords) > 1),
    PassSpec[PipelineHost, _PipelineRun]("fact_subscripted", run, when=lambda self, _: bool(self.subscripted)),
    PassSpec("argument", run, when=lambda _, state: state.args.with_import and not state.args.format_),
    PassSpec("configuration", run, when=lambda _, state: state.config.use_deferred_annotations),
    PassSpec("state_fact", run, when=lambda _, state: bool(state.module_models)),
    PassSpec("resolver_option", run, when=lambda self, _: self.run_context.records_model_names),
    PassSpec("resolver_whole", run, when=lambda self, _: bool(self.model_resolver)),
    PassSpec("resolver_by_name", run, when=lambda self, _: getattr(self, NAMES[0])),
    PassSpec("resolver_record", run, when=lambda self, _: bool(self.model_resolver.references)),
    PassSpec("listed_module_variable", run, when=lambda *_: _FLOORS.get() is not None),
    PassSpec("module_variable", run, when=lambda *_: bool(REGISTRY)),
    PassSpec("imported_run_state", run, when=lambda *_: _RUN_RECORD.get() is not None),
    PassSpec("option_through_a_helper", run, when=lambda self, _: helpers.reads_an_option(self)),
    PassSpec("fact_through_a_helper", run, when=lambda self, _: helpers.reads_a_fact(self)),
    PassSpec("option_function", run, when=an_option),
    PassSpec("fact_function", run, when=a_fact),
    PassSpec("fact_function_of_a_module", run, when=helpers.a_gate_of_a_module),
    PassSpec("no_when", lambda self, _: print(self.in_a_run)),
    ForEachModule(
        "each_module",
        lambda _, state: state.module_models,
        (
            PassSpec("option_in_a_module_loop", run, when=lambda self, _: self.use_annotated),
            PassSpec("fact_in_a_module_loop", run, when=lambda self, _: bool(self.in_a_module_loop)),
        ),
    ),
    Segment("segment", view, (PassSpec("fact_in_a_segment", run, when=lambda self, _: bool(self.in_a_segment)),)),
)
FINALIZE: tuple[PassSpec[PipelineHost, _FinalizeState], ...] = (
    PassSpec("finalize_option", run, when=lambda _, state: state.with_import),
    PassSpec("finalize_fact", run, when=lambda _, state: bool(state.contexts)),
)
