# ========= Copyright 2026 @ Strukto.AI 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.
# ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========

from collections.abc import AsyncIterator

from mirage.accessor.mongodb import MongoDBAccessor
from mirage.commands.builtin.mongodb._provision import file_read_provision
from mirage.commands.registry import command
from mirage.commands.spec import SPECS
from mirage.core.mongodb._client import count_documents
from mirage.core.mongodb.glob import resolve_glob
from mirage.core.mongodb.scope import detect_scope
from mirage.io.types import ByteSource, IOResult
from mirage.provision.types import ProvisionResult
from mirage.types import PathSpec


async def wc_provision(
    accessor: MongoDBAccessor,
    paths: list[PathSpec],
    *texts: str,
    **_extra: object,
) -> ProvisionResult:
    return await file_read_provision(
        accessor, paths,
        "wc " + " ".join(p.original if isinstance(p, PathSpec) else p
                         for p in paths))


def _is_single_db(config) -> bool:
    return (config.databases is not None and len(config.databases) == 1)


@command("wc", resource="mongodb", spec=SPECS["wc"], provision=wc_provision)
async def wc(
    accessor: MongoDBAccessor,
    paths: list[PathSpec],
    *texts: str,
    stdin: AsyncIterator[bytes] | bytes | None = None,
    args_l: bool = False,
    w: bool = False,
    c: bool = False,
    m: bool = False,
    L: bool = False,
    **_extra: object,
) -> tuple[ByteSource | None, IOResult]:
    if w or m or L:
        msg = "wc: only -l and -c supported for MongoDB"
        return None, IOResult(
            exit_code=1,
            stderr=msg.encode(),
        )

    if not paths:
        raise ValueError("wc: missing operand")

    single_db = _is_single_db(accessor.config)
    dbs = accessor.config.databases
    single_db_name = dbs[0] if single_db else None
    scope = detect_scope(
        paths[0],
        single_db=single_db,
        single_db_name=single_db_name,
    )

    if scope.level == "file" and scope.database and scope.collection:
        count = await count_documents(
            accessor.client,
            scope.database,
            scope.collection,
        )
        if c:
            paths = await resolve_glob(accessor, paths)
            from mirage.core.mongodb.read import read
            data = await read(
                accessor,
                paths[0].original,
                _extra.get("index"),
            )
            return str(len(data)).encode(), IOResult()
        return str(count).encode(), IOResult()

    raise ValueError("wc: path must target a collection file")
