package service

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

	"3588AdminBackend/internal/models"
	"3588AdminBackend/internal/storage"
)

func TestSyncStandardResourcesFromDirectory(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, "face_gallery_v1.db")
	if err := os.WriteFile(path, []byte("face-gallery-bytes"), 0o644); err != nil {
		t.Fatalf("WriteFile: %v", err)
	}

	store, err := storage.OpenSQLite(filepath.Join(t.TempDir(), "app.db"))
	if err != nil {
		t.Fatalf("OpenSQLite: %v", err)
	}
	defer store.Close()

	repo := storage.NewResourcesRepo(store.DB())
	svc := NewResourceManagementService(repo)

	if err := svc.SyncStandardResourcesFromDirectory(dir); err != nil {
		t.Fatalf("SyncStandardResourcesFromDirectory: %v", err)
	}

	items, err := repo.List()
	if err != nil {
		t.Fatalf("List: %v", err)
	}
	if len(items) != 1 || items[0].Name != "face_gallery_v1" {
		t.Fatalf("unexpected synced resources: %#v", items)
	}
	if items[0].SHA256 == "" {
		t.Fatalf("expected sha256 to be populated: %#v", items[0])
	}
}

func TestBuildResourceStatusBoardMarksMissingAndMismatch(t *testing.T) {
	resources := []storage.StandardResourceRecord{
		{Name: "face_gallery_v1", ResourceType: "face_gallery", SHA256: "sha-1", Version: "v1"},
		{Name: "ppe_dataset_v1", ResourceType: "dataset", SHA256: "sha-2", Version: "v2"},
	}
	devices := []*models.Device{
		{DeviceID: "edge-01", DeviceName: "设备一", Online: true},
	}
	installed := map[string][]InstalledResourceStatus{
		"edge-01": {
			{Name: "face_gallery_v1", ResourceType: "face_gallery", SHA256: "sha-1"},
		},
	}

	board := BuildResourceStatusBoard(resources, devices, installed)

	if board.Summary.MissingDevices != 1 {
		t.Fatalf("expected one device with missing resources, got %#v", board.Summary)
	}
	if len(board.Rows) != 1 || len(board.Rows[0].Cells) != 2 {
		t.Fatalf("unexpected board rows: %#v", board.Rows)
	}
	if board.Rows[0].Cells[1].Status != "missing" {
		t.Fatalf("expected second resource to be missing, got %#v", board.Rows[0].Cells[1])
	}
}

func TestBuildResourceStatusBoardSeparatesExtraResources(t *testing.T) {
	resources := []storage.StandardResourceRecord{
		{Name: "face_gallery_v1", ResourceType: "face_gallery", SHA256: "sha-1"},
	}
	devices := []*models.Device{
		{DeviceID: "edge-01", DeviceName: "设备一", Online: true},
	}
	installed := map[string][]InstalledResourceStatus{
		"edge-01": {
			{Name: "face_gallery_v1", ResourceType: "face_gallery", SHA256: "sha-1"},
			{Name: "extra_db", ResourceType: "face_gallery", SHA256: "sha-x"},
			{Name: "custom_ds", ResourceType: "dataset", SHA256: "sha-y"},
		},
	}

	board := BuildResourceStatusBoard(resources, devices, installed)

	if len(board.Rows) != 1 {
		t.Fatalf("unexpected board rows: %#v", board.Rows)
	}
	if got := board.Rows[0].ExtraCount; got != 2 {
		t.Fatalf("expected 2 extra resources, got %#v", board.Rows[0])
	}
	if len(board.Rows[0].ExtraResources) != 2 {
		t.Fatalf("expected extra resource details to be preserved, got %#v", board.Rows[0].ExtraResources)
	}
}
