"""Tests for UID conversion operations in file_ops."""

from __future__ import annotations

import os
import pytest

from godotiq.tools.bridge.file_ops import _op_uid_to_path, _op_path_to_uid


class TestUidToPath:
    def test_invalid_uid_format(self):
        result = _op_uid_to_path("not_a_uid")
        assert "error" in result

    def test_empty_uid(self):
        result = _op_uid_to_path("")
        assert "error" in result

    def test_uid_found_in_tscn(self, tmp_path, monkeypatch):
        """UID in .tscn ext_resource line should be resolved."""
        monkeypatch.setenv("GODOTIQ_PROJECT_ROOT", str(tmp_path))
        (tmp_path / "main.tscn").write_text(
            '[gd_scene format=3 uid="uid://abc123"]\n'
            '[ext_resource type="Script" uid="uid://def456" path="res://scripts/my.gd" id="1"]\n'
        )
        result = _op_uid_to_path("uid://def456")
        assert result["path"] == "res://scripts/my.gd"

    def test_uid_found_in_import(self, tmp_path, monkeypatch):
        """UID in .import file should resolve to source_file."""
        monkeypatch.setenv("GODOTIQ_PROJECT_ROOT", str(tmp_path))
        (tmp_path / "icon.png.import").write_text(
            '[remap]\n\nimporter="texture"\nuid="uid://xyz789"\n'
            'source_file="res://icon.png"\n'
        )
        result = _op_uid_to_path("uid://xyz789")
        assert result["path"] == "res://icon.png"

    def test_uid_not_found(self, tmp_path, monkeypatch):
        """Unknown UID should return error."""
        monkeypatch.setenv("GODOTIQ_PROJECT_ROOT", str(tmp_path))
        result = _op_uid_to_path("uid://nonexistent")
        assert result["path"] is None
        assert "error" in result


class TestPathToUid:
    def test_empty_path(self):
        result = _op_path_to_uid("")
        assert "error" in result

    def test_uid_from_uid_file(self, tmp_path, monkeypatch):
        """Should read .uid file next to resource."""
        monkeypatch.setenv("GODOTIQ_PROJECT_ROOT", str(tmp_path))
        (tmp_path / "scripts").mkdir()
        (tmp_path / "scripts" / "my.gd").write_text("extends Node\n")
        (tmp_path / "scripts" / "my.gd.uid").write_text("uid://abc123\n")
        result = _op_path_to_uid("res://scripts/my.gd")
        assert result["uid"] == "uid://abc123"

    def test_uid_from_tscn_reference(self, tmp_path, monkeypatch):
        """Should find UID in .tscn ext_resource line."""
        monkeypatch.setenv("GODOTIQ_PROJECT_ROOT", str(tmp_path))
        (tmp_path / "scripts").mkdir()
        (tmp_path / "scripts" / "my.gd").write_text("extends Node\n")
        (tmp_path / "main.tscn").write_text(
            '[ext_resource type="Script" uid="uid://found789" path="res://scripts/my.gd" id="1"]\n'
        )
        result = _op_path_to_uid("res://scripts/my.gd")
        assert result["uid"] == "uid://found789"

    def test_uid_not_found(self, tmp_path, monkeypatch):
        """File without UID should return error."""
        monkeypatch.setenv("GODOTIQ_PROJECT_ROOT", str(tmp_path))
        (tmp_path / "scripts").mkdir()
        (tmp_path / "scripts" / "my.gd").write_text("extends Node\n")
        result = _op_path_to_uid("res://scripts/my.gd")
        assert result["uid"] is None
        assert "error" in result
