import { ConfigService } from "@nestjs/config";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { GitOperationsService } from "./git-operations.service";
import { GitOperationError } from "./types";

// Mock simple-git
const mockGit = {
  clone: vi.fn(),
  checkoutLocalBranch: vi.fn(),
  add: vi.fn(),
  commit: vi.fn(),
  push: vi.fn(),
  addConfig: vi.fn(),
};

vi.mock("simple-git", () => ({
  simpleGit: vi.fn(() => mockGit),
}));

describe("GitOperationsService", () => {
  let service: GitOperationsService;
  let mockConfigService: ConfigService;

  beforeEach(() => {
    // Reset all mocks
    vi.clearAllMocks();

    // Create mock config service
    mockConfigService = {
      get: vi.fn((key: string) => {
        if (key === "orchestrator.git.userName") return "Test User";
        if (key === "orchestrator.git.userEmail") return "test@example.com";
        return undefined;
      }),
    } as unknown as ConfigService;

    // Create service with mock
    service = new GitOperationsService(mockConfigService);
  });

  describe("cloneRepository", () => {
    it("should clone a repository successfully", async () => {
      mockGit.clone.mockResolvedValue(undefined);

      await service.cloneRepository("https://github.com/test/repo.git", "/tmp/repo");

      expect(mockGit.clone).toHaveBeenCalledWith("https://github.com/test/repo.git", "/tmp/repo");
    });

    it("should clone a repository with specific branch", async () => {
      mockGit.clone.mockResolvedValue(undefined);

      await service.cloneRepository("https://github.com/test/repo.git", "/tmp/repo", "develop");

      expect(mockGit.clone).toHaveBeenCalledWith("https://github.com/test/repo.git", "/tmp/repo", [
        "--branch",
        "develop",
      ]);
    });

    it("should throw GitOperationError on clone failure", async () => {
      const error = new Error("Clone failed");
      mockGit.clone.mockRejectedValue(error);

      await expect(
        service.cloneRepository("https://github.com/test/repo.git", "/tmp/repo")
      ).rejects.toThrow(GitOperationError);

      try {
        await service.cloneRepository("https://github.com/test/repo.git", "/tmp/repo");
      } catch (e) {
        expect(e).toBeInstanceOf(GitOperationError);
        expect((e as GitOperationError).operation).toBe("clone");
        expect((e as GitOperationError).cause).toBe(error);
      }
    });
  });

  describe("createBranch", () => {
    it("should create and checkout a new branch", async () => {
      mockGit.checkoutLocalBranch.mockResolvedValue(undefined);

      await service.createBranch("/tmp/repo", "feature/new-branch");

      expect(mockGit.checkoutLocalBranch).toHaveBeenCalledWith("feature/new-branch");
    });

    it("should throw GitOperationError on branch creation failure", async () => {
      const error = new Error("Branch already exists");
      mockGit.checkoutLocalBranch.mockRejectedValue(error);

      await expect(service.createBranch("/tmp/repo", "feature/new-branch")).rejects.toThrow(
        GitOperationError
      );

      try {
        await service.createBranch("/tmp/repo", "feature/new-branch");
      } catch (e) {
        expect(e).toBeInstanceOf(GitOperationError);
        expect((e as GitOperationError).operation).toBe("createBranch");
        expect((e as GitOperationError).cause).toBe(error);
      }
    });
  });

  describe("commit", () => {
    it("should stage all changes and commit with message", async () => {
      mockGit.add.mockResolvedValue(undefined);
      mockGit.commit.mockResolvedValue({ commit: "abc123" });

      await service.commit("/tmp/repo", "feat: add new feature");

      expect(mockGit.add).toHaveBeenCalledWith(".");
      expect(mockGit.commit).toHaveBeenCalledWith("feat: add new feature");
    });

    it("should stage specific files when provided", async () => {
      mockGit.add.mockResolvedValue(undefined);
      mockGit.commit.mockResolvedValue({ commit: "abc123" });

      await service.commit("/tmp/repo", "fix: update files", ["file1.ts", "file2.ts"]);

      expect(mockGit.add).toHaveBeenCalledWith(["file1.ts", "file2.ts"]);
      expect(mockGit.commit).toHaveBeenCalledWith("fix: update files");
    });

    it("should configure git user before committing", async () => {
      mockGit.add.mockResolvedValue(undefined);
      mockGit.commit.mockResolvedValue({ commit: "abc123" });
      mockGit.addConfig.mockResolvedValue(undefined);

      await service.commit("/tmp/repo", "test commit");

      expect(mockGit.addConfig).toHaveBeenCalledWith("user.name", "Test User");
      expect(mockGit.addConfig).toHaveBeenCalledWith("user.email", "test@example.com");
    });

    it("should throw GitOperationError on commit failure", async () => {
      mockGit.add.mockResolvedValue(undefined);
      const error = new Error("Nothing to commit");
      mockGit.commit.mockRejectedValue(error);

      await expect(service.commit("/tmp/repo", "test commit")).rejects.toThrow(GitOperationError);

      try {
        await service.commit("/tmp/repo", "test commit");
      } catch (e) {
        expect(e).toBeInstanceOf(GitOperationError);
        expect((e as GitOperationError).operation).toBe("commit");
        expect((e as GitOperationError).cause).toBe(error);
      }
    });
  });

  describe("push", () => {
    it("should push to origin and current branch by default", async () => {
      mockGit.push.mockResolvedValue(undefined);

      await service.push("/tmp/repo");

      expect(mockGit.push).toHaveBeenCalledWith("origin", undefined);
    });

    it("should push to specified remote and branch", async () => {
      mockGit.push.mockResolvedValue(undefined);

      await service.push("/tmp/repo", "upstream", "main");

      expect(mockGit.push).toHaveBeenCalledWith("upstream", "main");
    });

    it("should support force push", async () => {
      mockGit.push.mockResolvedValue(undefined);

      await service.push("/tmp/repo", "origin", "develop", true);

      expect(mockGit.push).toHaveBeenCalledWith("origin", "develop", {
        "--force": null,
      });
    });

    it("should throw GitOperationError on push failure", async () => {
      const error = new Error("Push rejected");
      mockGit.push.mockRejectedValue(error);

      await expect(service.push("/tmp/repo")).rejects.toThrow(GitOperationError);

      try {
        await service.push("/tmp/repo");
      } catch (e) {
        expect(e).toBeInstanceOf(GitOperationError);
        expect((e as GitOperationError).operation).toBe("push");
        expect((e as GitOperationError).cause).toBe(error);
      }
    });
  });

  describe("git config", () => {
    it("should read git config from ConfigService", () => {
      expect(mockConfigService.get("orchestrator.git.userName")).toBe("Test User");
      expect(mockConfigService.get("orchestrator.git.userEmail")).toBe("test@example.com");
    });
  });
});
