# This file was auto-generated by Fern from our API Definition.

from __future__ import annotations

import typing

import pydantic
import typing_extensions
from ..core.pydantic_utilities import IS_PYDANTIC_V2, update_forward_refs
from ..core.serialization import FieldMetadata
from ..core.unchecked_base_model import UncheckedBaseModel
from .created_at import CreatedAt
from .extract_config import ExtractConfig
from .extract_output import ExtractOutput
from .extract_output_edits import ExtractOutputEdits
from .extractor_summary import ExtractorSummary
from .extractor_version_summary import ExtractorVersionSummary
from .file_summary import FileSummary
from .processor_run_status import ProcessorRunStatus
from .run_metadata import RunMetadata
from .run_usage import RunUsage
from .updated_at import UpdatedAt


class ExtractRun(UncheckedBaseModel):
    """
    Extract run object.
    """

    object: typing.Literal["extract_run"] = pydantic.Field(default="extract_run")
    """
    The type of object. Will always be `"extract_run"`.
    """

    id: str = pydantic.Field()
    """
    The unique identifier for this extract run.
    
    Example: `"exr_Xj8mK2pL9nR4vT7qY5wZ"`
    """

    extractor: typing.Optional[ExtractorSummary] = pydantic.Field(default=None)
    """
    The extractor that was used for this run.
    
    **Availability:** Present when an extractor reference was provided. Not present when using inline `config`.
    """

    extractor_version: typing_extensions.Annotated[
        typing.Optional[ExtractorVersionSummary], FieldMetadata(alias="extractorVersion")
    ] = pydantic.Field(alias="extractorVersion", default=None)
    """
    The version of the extractor that was used for this run.
    
    **Availability:** Present when an extractor reference was provided. Not present when using inline `config`.
    """

    status: ProcessorRunStatus
    output: typing.Optional[ExtractOutput] = pydantic.Field(default=None)
    """
    The final output, either reviewed or initial. This is a union of two possible shapes:
    
    - **[JSON Schema output](https://docs.extend.ai/2026-02-09/product/extraction/output-types):** The current output format, returned for runs created with a JSON Schema config.
    - **[Legacy output](https://docs.extend.ai/2025-04-21/product/legacy/output-type-legacy):** A legacy output format from a previous API version. This shape is only returned for runs that were originally created with a legacy config.
    
    **Availability:** Present when `status` is `"PROCESSED"`.
    """

    initial_output: typing_extensions.Annotated[
        typing.Optional[ExtractOutput], FieldMetadata(alias="initialOutput")
    ] = pydantic.Field(alias="initialOutput", default=None)
    """
    The initial output from the extract run, before any review edits.
    
    **Availability:** Present when `reviewed` is `true`.
    """

    reviewed_output: typing_extensions.Annotated[
        typing.Optional[ExtractOutput], FieldMetadata(alias="reviewedOutput")
    ] = pydantic.Field(alias="reviewedOutput", default=None)
    """
    The output after human review.
    
    **Availability:** Present when `reviewed` is `true`.
    """

    failure_reason: typing_extensions.Annotated[typing.Optional[str], FieldMetadata(alias="failureReason")] = (
        pydantic.Field(alias="failureReason", default=None)
    )
    """
    The reason for failure.
    
    **Availability:** Present when `status` is `"FAILED"`.
    
    Possible values include:
    * `ABORTED` - The run was aborted by the user
    * `INTERNAL_ERROR` - An unexpected internal error occurred
    * `FAILED_TO_PROCESS_FILE` - Failed to process the file (e.g., OCR failure, file access issues)
    * `INVALID_PROCESSOR` - The processor configuration is invalid
    * `INVALID_CONFIGURATION` - The provided configuration is incompatible with the selected model
    * `PARSING_ERROR` - Failed to parse the extraction output
    * `PRE_PROCESSING_FAILURE` - An error occurred during preprocessing (e.g., chunking)
    * `POST_PROCESSING_FAILURE` - An error occurred during postprocessing
    * `OUT_OF_CREDITS` - Insufficient credits to run the extraction
    
    **Note:** Additional failure reasons may be added in the future. Your integration should handle unknown values gracefully.
    """

    failure_message: typing_extensions.Annotated[typing.Optional[str], FieldMetadata(alias="failureMessage")] = (
        pydantic.Field(alias="failureMessage", default=None)
    )
    """
    A detailed message about the failure.
    
    **Availability:** Present when `status` is `"FAILED"`.
    """

    metadata: typing.Optional[RunMetadata] = pydantic.Field(default=None)
    """
    Any metadata that was provided when creating the extract run.
    
    **Availability:** Present when metadata was provided during creation.
    """

    reviewed: bool = pydantic.Field()
    """
    Indicates whether the run has been reviewed by a human.
    """

    edited: bool = pydantic.Field()
    """
    Indicates whether the run results have been edited during review.
    """

    edits: typing.Optional[typing.Dict[str, typing.Optional[ExtractOutputEdits]]] = pydantic.Field(default=None)
    """
    Details of edits made during review.
    
    **Availability:** Present when `edited` is `true`.
    """

    config: ExtractConfig = pydantic.Field()
    """
    The configuration used for this extract run. This is a union of two possible shapes:
    
    - **[JSON Schema config](https://docs.extend.ai/2026-02-09/product/extraction/schema):** The current config format. All runs created through this API version use this shape.
    - **[Legacy config](https://docs.extend.ai/2025-04-21/product/legacy/legacy-schema):** A fields-array config from a previous API version. This shape is only returned when retrieving runs that were originally created with the legacy format. This API version does not support creating runs with legacy configs.
    """

    file: FileSummary = pydantic.Field()
    """
    The file that was processed.
    """

    parse_run_id: typing_extensions.Annotated[typing.Optional[str], FieldMetadata(alias="parseRunId")] = pydantic.Field(
        alias="parseRunId", default=None
    )
    """
    The ID of the parse run that was used for this extract run.
    
    **Availability:** Present when a parse run was created.
    """

    dashboard_url: typing_extensions.Annotated[str, FieldMetadata(alias="dashboardUrl")] = pydantic.Field(
        alias="dashboardUrl"
    )
    """
    The URL to view the extract run in the Extend dashboard.
    """

    usage: typing.Optional[RunUsage] = pydantic.Field(default=None)
    """
    Usage credits consumed by this run.
    
    **Availability:** Present when `status` is `"PROCESSED"`.
    """

    created_at: typing_extensions.Annotated[CreatedAt, FieldMetadata(alias="createdAt")] = pydantic.Field(
        alias="createdAt"
    )
    updated_at: typing_extensions.Annotated[UpdatedAt, FieldMetadata(alias="updatedAt")] = pydantic.Field(
        alias="updatedAt"
    )

    if IS_PYDANTIC_V2:
        model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(extra="allow", frozen=True)  # type: ignore # Pydantic v2
    else:

        class Config:
            frozen = True
            smart_union = True
            extra = pydantic.Extra.allow


update_forward_refs(ExtractRun)
