import Foundation
import SaplingCore

public final class MockGitProvider: GitProvider {
    public let displayName = "Mock Git"
    private let statuses: [GitFileStatus]

    public init(
        statuses: [GitFileStatus] = [
            GitFileStatus(path: "README.md", state: .modified),
            GitFileStatus(path: "attachments/architecture.png", state: .untracked)
        ]
    ) {
        self.statuses = statuses
    }

    public func initRepository(at path: URL) async throws -> GitRepository {
        GitRepository(name: path.lastPathComponent, rootURL: path)
    }

    public func clone(remoteURL: URL, to destinationURL: URL) async throws -> GitRepository {
        GitRepository(
            name: destinationURL.lastPathComponent,
            rootURL: destinationURL,
            remotes: [GitRemote(name: "origin", url: remoteURL)]
        )
    }

    public func repository(at path: URL) -> any Repository {
        MockRepository(rootURL: path, statuses: statuses)
    }
}

private struct MockRepository: Repository {
    let rootURL: URL
    let statuses: [GitFileStatus]

    func status() async throws -> [GitFileStatus] { statuses }
    func add(paths: [String]) async throws {}

    func commit(message: String) async throws -> GitCommit {
        GitCommit(
            id: UUID().uuidString.replacingOccurrences(of: "-", with: "").lowercased(),
            shortHash: "mocked",
            authorName: "Sapling",
            authorEmail: "sapling@example.com",
            message: message,
            authoredAt: Date()
        )
    }

    func push(remote: String?, branch: String?) async throws {}
    func pull(remote: String?, branch: String?) async throws {}
    func createBranch(named name: String) async throws -> GitBranch { GitBranch(name: name) }
    func switchBranch(named name: String) async throws -> GitBranch { GitBranch(name: name, isCurrent: true) }
    func merge(branch name: String) async throws {}

    func addSubmodule(remoteURL: URL, path: String) async throws -> Subproject {
        Subproject(
            name: URL(fileURLWithPath: path).lastPathComponent,
            path: path,
            repositoryURL: rootURL.appendingPathComponent(path),
            remoteURL: remoteURL
        )
    }

    func removeSubmodule(path: String) async throws {}
    func updateSubmodules(initSubmodules: Bool, recursive: Bool) async throws {}
    func syncSubmodules(recursive: Bool) async throws {}
}
