# Copyright 2023 The Pigweed Authors
#
# 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
#
#     https://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.
"""Bazel-related functions."""

import contextlib
import dataclasses
import pprint
from typing import Any, Dict, Optional

from PB.recipe_modules.pigweed.bazel.options import Options

import attr
from recipe_engine import config_types, recipe_api


@dataclasses.dataclass
class BazelRunner:
    api: Any
    checkout_root: config_types.Path
    options: Options
    bazel: Optional[config_types.Path] = None

    def ensure(self) -> config_types.Path:
        if self.bazel:
            return self.bazel

        # read_json in a few lines requires absolute, normalized paths.
        json_path = self.api.path.abspath(
            self.checkout_root / self.options.cipd_json_path
        )

        ensure_file = self.api.cipd.EnsureFile()
        for package in self.api.file.read_json(
            f'read {self.api.path.basename(json_path)}',
            json_path,
            test_data=_JSON_TEST_DATA,
        )["packages"]:
            ensure_file.add_package(package['path'], package['tags'][0])

        root = self.api.path.mkdtemp()
        self.api.cipd.ensure(root, ensure_file, name='ensure bazel')
        self.bazel = root / 'bazel'
        return self.bazel

    def run(self, **kwargs):
        name = ' '.join(['bazel'] + list(self.options.args))
        with self.api.context(cwd=self.checkout_root):
            return self.api.step(
                name, [self.ensure(), *self.options.args], **kwargs
            )


# This is copied from bazel.json in Pigweed, but there's no need to keep it
# up-to-date.
_JSON_TEST_DATA = {
    "included_files": [],
    "packages": [
        {
            "path": "fuchsia/third_party/bazel/${platform}",
            "platforms": ["linux-amd64", "mac-amd64", "windows-amd64"],
            "tags": ["version:2@6.3.0.6"],
            "version_file": ".versions/bazel.cipd_version",
        },
        {
            "path": "flutter/java/openjdk/${platform}",
            "platforms": [
                "linux-amd64",
                "mac-amd64",
                "mac-arm64",
                "windows-amd64",
            ],
            "tags": ["version:17"],
        },
    ],
}


class BazelApi(recipe_api.RecipeApi):
    """Bazel utilities."""

    def new_runner(self, checkout: Any, options: Options) -> BazelRunner:
        return BazelRunner(self.m, checkout.root, options)
