package main

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

func TestCreateIniFile_Unit(t *testing.T) {
	t.Parallel()
	t.Log("✓ Fast createIniFile unit test")

	tests := []struct {
		name     string
		fpath    string
		setup    func(string)
		wantLen  int
		wantFile bool
	}{
		{
			name:    "happy path",
			fpath:   "testdata/config.ini",
			wantLen: 0,
			wantFile: true,
		},
		{
			name:    "dir already exists",
			fpath:   "testdata/config.ini",
			setup: func(dir string) {
				os.MkdirAll(dir, 0770)
			},
			wantLen: 0,
			wantFile: true,
		},
		{
			name:    "deep nested dir",
			fpath:   "testdata/nested/sub/dir/config.ini",
			wantLen: 0,
			wantFile: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			dir := t.TempDir()
			fpath := filepath.Join(dir, tt.fpath)
			
			origWd, err := os.Getwd()
			if err != nil {
				t.Fatalf("failed to get working dir: %v", err)
			}
			if err := os.Chdir(dir); err != nil {
				t.Fatalf("failed to chdir: %v", err)
			}
			defer func() {
				if err := os.Chdir(origWd); err != nil {
					t.Errorf("failed to restore working dir: %v", err)
				}
			}()

			if tt.setup != nil {
				tt.setup(filepath.Dir(fpath))
			}

			errs := createIniFile(fpath)
			if len(errs) != tt.wantLen {
				t.Errorf("createIniFile() got %d errors = %v, want len %d", len(errs), errs, tt.wantLen)
			}

			exists := false
			if tt.wantFile {
				exists = true
				if _, err := os.Stat(fpath); err != nil {
					if os.IsNotExist(err) {
						exists = false
					} else {
						t.Errorf("stat(%q) unexpected error: %v", fpath, err)
					}
				}
			}

			if exists != tt.wantFile {
				t.Errorf("file exists = %v, want %v", exists, tt.wantFile)
			}

			if tt.wantFile {
				data, err := os.ReadFile(fpath)
				if err != nil {
					t.Errorf("failed to read created file: %v", err)
					return
				}
				const wantContent = `[DEFAULT]
radio_browser.api=all.api.radio-browser.info
player.command=mpv
player.options=--no-video
menu_items.max=9999
`
				if string(data) != wantContent {
					t.Errorf("file content mismatch:\ngot:  %q\nwant: %q", string(data), wantContent)
				}
			}
		})
	}
}

func TestCreateIniFile_Live(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping live integration test (use without -short)")
	}
	t.Log("🧪 Running live integration test...")

	dir := t.TempDir()
	fpath := filepath.Join(dir, "live_config.ini")
	t.Logf("🧪 Testing with real file path: %s", fpath)

	errs := createIniFile(fpath)
	t.Logf("🧪 createIniFile returned %d errors", len(errs))

	if len(errs) > 0 {
		t.Errorf("unexpected errors: %v", errs)
		return
	}

	data, err := os.ReadFile(fpath)
	if err != nil {
		t.Fatalf("failed to read created file: %v", err)
	}

	const wantContent = `[DEFAULT]
radio_browser.api=all.api.radio-browser.info
player.command=mpv
player.options=--no-video
menu_items.max=9999
`
	if string(data) != wantContent {
		t.Errorf("file content mismatch:\ngot:  %q\nwant: %q", string(data), wantContent)
	}

	t.Logf("🧪 Live test PASSED: file created successfully with correct content")
}