# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Model file paths and format resolution for inference model directory convention."""

from __future__ import annotations

from os import PathLike
from pathlib import Path
from typing import Literal, Tuple, TypedDict, Union, cast, get_args

from typing_extensions import TypeAlias

from ....constants import MODEL_FILE_PREFIX

LocalModelFormat: TypeAlias = Literal[
    "paddle",
    "onnx",
    "om",
    "paddle_dyn",
    "safetensors",
]

LOCAL_MODEL_FORMATS: Tuple[LocalModelFormat, ...] = cast(
    Tuple[LocalModelFormat, ...], get_args(LocalModelFormat)
)


class ModelPaths(TypedDict, total=False):
    """Resolved local model files keyed by `LocalModelFormat`."""

    paddle: Tuple[Path, Path]
    onnx: Path
    om: Path
    paddle_dyn: Path
    safetensors: Path


def get_model_paths(
    model_dir: Union[str, PathLike],
    model_file_prefix: str = MODEL_FILE_PREFIX,
) -> ModelPaths:
    model_dir = Path(model_dir)
    model_paths: ModelPaths = {}
    pd_model_path = None
    if (model_dir / f"{model_file_prefix}.json").exists():
        pd_model_path = model_dir / f"{model_file_prefix}.json"
    elif (model_dir / f"{model_file_prefix}.pdmodel").exists():
        pd_model_path = model_dir / f"{model_file_prefix}.pdmodel"
    if pd_model_path and (model_dir / f"{model_file_prefix}.pdiparams").exists():
        model_paths["paddle"] = (
            pd_model_path,
            model_dir / f"{model_file_prefix}.pdiparams",
        )
    if (model_dir / f"{model_file_prefix}.onnx").exists():
        model_paths["onnx"] = model_dir / f"{model_file_prefix}.onnx"
    if (model_dir / f"{model_file_prefix}.om").exists():
        model_paths["om"] = model_dir / f"{model_file_prefix}.om"
    if (model_dir / "model_state.pdparams").exists():
        model_paths["paddle_dyn"] = model_dir / "model_state.pdparams"
    if (model_dir / "inference.pdparams").exists():
        model_paths["paddle_dyn"] = model_dir / "inference.pdparams"
    if (model_dir / "model.safetensors").exists():
        model_paths["safetensors"] = model_dir / "model.safetensors"
    return model_paths


def resolve_paddle_engine_from_model_files(
    model_dir: Union[str, PathLike, Path],
    model_file_prefix: str = MODEL_FILE_PREFIX,
) -> str | None:
    """Resolve paddle_static vs paddle_dynamic from actual model files.

    Returns:
        "paddle_static", "paddle_dynamic", or None if no paddle model files found.
    """
    paths = get_model_paths(Path(model_dir), model_file_prefix)
    if "paddle" in paths:
        return "paddle_static"
    if "safetensors" in paths:
        return "paddle_dynamic"
    if "paddle_dyn" in paths:
        return "paddle_dynamic"
    return None
