import marimo

__generated_with = "0.23.14"
app = marimo.App(width="medium")


@app.cell(hide_code=True)
def _():
    from pathlib import Path

    import marimo as mo
    from reader_workbench.api import read_artifact, read_dataframe, records, verify
    from reader_workbench.api.notebooks import (
        build_notebook_deliverable_selector,
        build_notebook_overview,
        collect_notebook_deliverables,
        load_notebook_context,
        render_notebook_deliverable_viewport,
        render_notebook_overview_panel,
    )

    return (
        Path,
        mo,
        read_artifact,
        read_dataframe,
        records,
        verify,
        build_notebook_deliverable_selector,
        build_notebook_overview,
        collect_notebook_deliverables,
        load_notebook_context,
        render_notebook_deliverable_viewport,
        render_notebook_overview_panel,
    )


@app.cell(hide_code=True)
def _(Path, build_notebook_overview, load_notebook_context):
    notebook_context = load_notebook_context(Path(__file__).resolve())
    experiment = notebook_context.experiment
    outputs_dir = notebook_context.outputs_dir
    notebooks_dir = notebook_context.notebooks_dir
    notebook_overview = build_notebook_overview(
        experiment_id=experiment.identity.id,
        experiment_title=experiment.identity.title or "",
        protocol_id=experiment.identity.protocol,
        experiment_root=notebook_context.experiment_root,
        outputs_dir=outputs_dir,
        notebooks_dir=notebooks_dir,
        pipeline_step_ids=notebook_context.pipeline_step_ids,
    )
    return experiment, notebook_overview, outputs_dir


@app.cell(hide_code=True)
def _(experiment, records, verify):
    _record_catalog = records(experiment)
    _verification = verify(experiment)
    record_entries = _record_catalog.entries
    verification_issues = _verification.issues
    verification_records = _verification.records
    verification_status = _verification.status
    return record_entries, verification_issues, verification_records, verification_status


@app.cell(hide_code=True)
def _(
    collect_notebook_deliverables,
    outputs_dir,
    record_entries,
    verification_issues,
    verification_records,
    verification_status,
):
    deliverables = collect_notebook_deliverables(
        record_entries,
        outputs_dir=outputs_dir,
        verification_status=verification_status,
        verification_issues=verification_issues,
        verification_records=verification_records,
    )
    return (deliverables,)


@app.cell(hide_code=True)
def _(mo, notebook_overview, render_notebook_overview_panel):
    eda_overview_panel = render_notebook_overview_panel(mo, notebook_overview)
    return (eda_overview_panel,)


@app.cell(hide_code=True)
def _(build_notebook_deliverable_selector, deliverables, mo):
    deliverable_selector = build_notebook_deliverable_selector(mo, deliverables)
    return (deliverable_selector,)


@app.cell(hide_code=True)
def _(
    deliverable_selector,
    deliverables,
    experiment,
    mo,
    read_artifact,
    read_dataframe,
    render_notebook_deliverable_viewport,
):
    def _load_dataframe(_record_id, _revision, _revision_digest):
        return read_dataframe(
            experiment,
            _record_id,
            revision=_revision,
            revision_digest=_revision_digest,
            row_limit=200,
        ).dataframe

    def _load_artifact(_record_id, _revision, _revision_digest, _path):
        return read_artifact(
            experiment,
            _record_id,
            revision=_revision,
            revision_digest=_revision_digest,
            path=_path,
        ).content

    deliverables_panel = render_notebook_deliverable_viewport(
        mo,
        deliverables,
        deliverable_selector,
        dataframe_loader=_load_dataframe,
        artifact_loader=_load_artifact,
    )
    return (deliverables_panel,)


@app.cell(hide_code=True)
def _(deliverables_panel, eda_overview_panel, mo):
    notebook_panel = mo.vstack(
        [
            eda_overview_panel,
            deliverables_panel,
        ]
    )
    return (notebook_panel,)


@app.cell(hide_code=True)
def _(notebook_panel):
    notebook_panel


if __name__ == "__main__":
    app.run()
