package git

import (
	"os"
	"path/filepath"
	"testing"
)

func TestParseRemoteURL(t *testing.T) {
	tests := []struct {
		name      string
		url       string
		wantOwner string
		wantName  string
		wantHost  string
		wantErr   bool
	}{
		{
			name:      "HTTPS URL with .git",
			url:       "https://codeberg.org/romaintb/fj.git",
			wantOwner: "romaintb",
			wantName:  "fj",
			wantHost:  "codeberg.org",
			wantErr:   false,
		},
		{
			name:      "HTTPS URL without .git",
			url:       "https://codeberg.org/romaintb/fj",
			wantOwner: "romaintb",
			wantName:  "fj",
			wantHost:  "codeberg.org",
			wantErr:   false,
		},
		{
			name:      "SSH URL with .git",
			url:       "git@codeberg.org:romaintb/fj.git",
			wantOwner: "romaintb",
			wantName:  "fj",
			wantHost:  "codeberg.org",
			wantErr:   false,
		},
		{
			name:      "SSH URL without .git",
			url:       "git@codeberg.org:romaintb/fj",
			wantOwner: "romaintb",
			wantName:  "fj",
			wantHost:  "codeberg.org",
			wantErr:   false,
		},
		{
			name:      "SSH protocol URL",
			url:       "ssh://git@codeberg.org/romaintb/fj.git",
			wantOwner: "romaintb",
			wantName:  "fj",
			wantHost:  "codeberg.org",
			wantErr:   false,
		},
		{
			name:      "GitHub HTTPS URL",
			url:       "https://github.com/user/repo.git",
			wantOwner: "user",
			wantName:  "repo",
			wantHost:  "github.com",
			wantErr:   false,
		},
		{
			name:    "Invalid URL",
			url:     "invalid-url",
			wantErr: true,
		},
		{
			name:    "Empty URL",
			url:     "",
			wantErr: true,
		},
		{
			name:      "URL with trailing whitespace",
			url:       "  https://codeberg.org/owner/repo.git  ",
			wantOwner: "owner",
			wantName:  "repo",
			wantHost:  "codeberg.org",
			wantErr:   false,
		},
		{
			name:      "URL with port number",
			url:       "https://git.example.com:443/owner/repo.git",
			wantOwner: "owner",
			wantName:  "repo",
			wantHost:  "git.example.com:443",
			wantErr:   false,
		},
		{
			name:      "SSH URL with port parses incorrectly",
			url:       "ssh://git@git.example.com:22/owner/repo.git",
			wantOwner: "22",
			wantName:  "owner/repo",
			wantHost:  "git.example.com",
			wantErr:   false,
		},
		{
			name:      "HTTP URL (not HTTPS)",
			url:       "http://codeberg.org/owner/repo",
			wantOwner: "owner",
			wantName:  "repo",
			wantHost:  "codeberg.org",
			wantErr:   false,
		},
		{
			name:      "Repo name with dashes",
			url:       "https://codeberg.org/owner/my-cool-repo.git",
			wantOwner: "owner",
			wantName:  "my-cool-repo",
			wantHost:  "codeberg.org",
			wantErr:   false,
		},
		{
			name:      "Repo name with dots",
			url:       "https://codeberg.org/owner/my.repo.name.git",
			wantOwner: "owner",
			wantName:  "my.repo.name",
			wantHost:  "codeberg.org",
			wantErr:   false,
		},
		{
			name:      "Owner with dots",
			url:       "https://codeberg.org/owner.name/repo.git",
			wantOwner: "owner.name",
			wantName:  "repo",
			wantHost:  "codeberg.org",
			wantErr:   false,
		},
		{
			name:    "Missing owner/repo",
			url:     "https://codeberg.org/",
			wantErr: true,
		},
		{
			name:    "Only owner, no repo",
			url:     "https://codeberg.org/owner",
			wantErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			owner, name, host, err := parseRemoteURL(tt.url)
			if (err != nil) != tt.wantErr {
				t.Errorf("parseRemoteURL() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !tt.wantErr {
				if owner != tt.wantOwner {
					t.Errorf("parseRemoteURL() owner = %v, want %v", owner, tt.wantOwner)
				}
				if name != tt.wantName {
					t.Errorf("parseRemoteURL() name = %v, want %v", name, tt.wantName)
				}
				if host != tt.wantHost {
					t.Errorf("parseRemoteURL() host = %v, want %v", host, tt.wantHost)
				}
			}
		})
	}
}

func TestDetectHost(t *testing.T) {
	tests := []struct {
		name      string
		gitConfig string
		wantHost  string
		wantErr   bool
	}{
		{
			name: "HTTPS URL",
			gitConfig: `[core]
	repositoryformatversion = 0
[remote "origin"]
	url = https://codeberg.org/owner/repo.git
	fetch = +refs/heads/*:refs/remotes/origin/*
`,
			wantHost: "codeberg.org",
			wantErr:  false,
		},
		{
			name: "SSH URL",
			gitConfig: `[core]
	repositoryformatversion = 0
[remote "origin"]
	url = git@codeberg.org:owner/repo.git
	fetch = +refs/heads/*:refs/remotes/origin/*
`,
			wantHost: "codeberg.org",
			wantErr:  false,
		},
		{
			name: "HTTPS URL with port",
			gitConfig: `[core]
	repositoryformatversion = 0
[remote "origin"]
	url = https://git.example.com:443/owner/repo.git
	fetch = +refs/heads/*:refs/remotes/origin/*
`,
			wantHost: "git.example.com:443",
			wantErr:  false,
		},
		{
			name: "SSH protocol URL",
			gitConfig: `[core]
	repositoryformatversion = 0
[remote "origin"]
	url = ssh://git@codeberg.org/owner/repo.git
	fetch = +refs/heads/*:refs/remotes/origin/*
`,
			wantHost: "codeberg.org",
			wantErr:  false,
		},
		{
			name: "No origin remote",
			gitConfig: `[core]
	repositoryformatversion = 0
[remote "upstream"]
	url = https://codeberg.org/owner/repo.git
	fetch = +refs/heads/*:refs/remotes/origin/*
`,
			wantHost: "",
			wantErr:  true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tmpDir := t.TempDir()
			gitDir := filepath.Join(tmpDir, ".git")
			if err := os.MkdirAll(gitDir, 0755); err != nil {
				t.Fatalf("Failed to create .git directory: %v", err)
			}

			configPath := filepath.Join(gitDir, "config")
			if err := os.WriteFile(configPath, []byte(tt.gitConfig), 0644); err != nil {
				t.Fatalf("Failed to write git config: %v", err)
			}

			oldWd, err := os.Getwd()
			if err != nil {
				t.Fatalf("Failed to get current directory: %v", err)
			}
			defer func() {
				if err := os.Chdir(oldWd); err != nil {
					t.Logf("Failed to change directory back: %v", err)
				}
			}()

			if err := os.Chdir(tmpDir); err != nil {
				t.Fatalf("Failed to change to temp directory: %v", err)
			}

			host, err := DetectHost()
			if (err != nil) != tt.wantErr {
				t.Errorf("DetectHost() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !tt.wantErr && host != tt.wantHost {
				t.Errorf("DetectHost() host = %v, want %v", host, tt.wantHost)
			}
		})
	}
}
