# 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 .file_summary import FileSummary
from .parse_config import ParseConfig
from .parse_run_metrics import ParseRunMetrics
from .parse_run_output import ParseRunOutput
from .parse_run_status_enum import ParseRunStatusEnum
from .run_usage import RunUsage


class ParseRun(UncheckedBaseModel):
    """
    Parse run object.
    """

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

    id: str = pydantic.Field()
    """
    A unique identifier for the parse run.
    
    Example: `"pr_xK9mLPqRtN3vS8wF5hB2cQ"`
    """

    file: FileSummary = pydantic.Field()
    """
    The file that was parsed. This file can be used as a parameter for other Extend endpoints, such as `POST /workflow_runs`.
    """

    status: ParseRunStatusEnum = pydantic.Field()
    """
    The status of the parse run:
    * `"PROCESSING"` - The file is still being processed
    * `"PROCESSED"` - The file was successfully processed
    * `"FAILED"` - The processing failed (see `failureReason` for details)
    """

    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:
    * `UNABLE_TO_DOWNLOAD_FILE` - The file could not be downloaded from the provided URL
    * `FILE_TYPE_NOT_SUPPORTED` - The file type is not supported for parsing
    * `FILE_SIZE_TOO_LARGE` - The file exceeds the maximum allowed size
    * `CORRUPT_FILE` - The file appears to be corrupted or malformed
    * `OCR_ERROR` - An error occurred during optical character recognition
    * `PASSWORD_PROTECTED_FILE` - The file is password protected and cannot be processed
    * `FAILED_TO_CONVERT_TO_PDF` - The file could not be converted to PDF for processing
    * `FAILED_TO_CONVERT_TO_JPEG` - The file could not be converted to JPEG for processing
    * `FAILED_TO_GENERATE_TARGET_FORMAT` - The output could not be generated in the requested format
    * `CHUNKING_ERROR` - An error occurred while chunking the document
    * `INTERNAL_ERROR` - An unexpected internal error occurred
    * `INVALID_CONFIG_OPTIONS` - The provided configuration options are invalid
    * `OUT_OF_CREDITS` - Insufficient credits to process the file
    
    **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 human-readable description of the failure.
    
    **Availability:** Present when `status` is `"FAILED"`.
    """

    output: typing.Optional[ParseRunOutput] = pydantic.Field(default=None)
    """
    The parse run output.
    
    **Availability:** Present when `status` is `"PROCESSED"` and the request was made without the `responseType=url` query parameter. Contains the parsed chunks.
    """

    output_url: typing_extensions.Annotated[typing.Optional[str], FieldMetadata(alias="outputUrl")] = pydantic.Field(
        alias="outputUrl", default=None
    )
    """
    A presigned URL to download the parse run output as a JSON file. The object shape is the same as the `output` field. Expires after 15 minutes.
    
    **Availability:** Present when `status` is `"PROCESSED"` and the request was made with `responseType=url` query parameter.
    """

    metrics: typing.Optional[ParseRunMetrics] = pydantic.Field(default=None)
    """
    Metrics about the parsing process.
    
    **Availability:** Present when `status` is `"PROCESSED"`.
    """

    config: ParseConfig = pydantic.Field()
    """
    The configuration used for the parsing process, including any default values that were applied.
    """

    usage: typing.Optional[RunUsage] = pydantic.Field(default=None)
    """
    Usage credits consumed by this run.
    
    **Availability:** Present when `status` is `"PROCESSED"`, the run was created after October 7, 2025, and the customer is on the current billing system.
    """

    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(ParseRun)
