package nextcloud

import (
	"strings"
	"testing"
)

func TestDecodeOCSShareRecordsArray(t *testing.T) {
	raw := `[{"id":"6","share_type":3,"permissions":17,"path":"/docs","share_with":null,"stime":1780572446}]`
	items, err := decodeOCSShareRecords([]byte(raw))
	if err != nil {
		t.Fatal(err)
	}
	if len(items) != 1 {
		t.Fatalf("len = %d, want 1", len(items))
	}
	if items[0].ID.String() != "6" {
		t.Fatalf("id = %q", items[0].ID)
	}
}

func TestDecodeOCSShareRecordsSingleObject(t *testing.T) {
	raw := `{"id":12,"share_type":0,"permissions":1,"path":"/file.txt","share_with":"admin"}`
	items, err := decodeOCSShareRecords([]byte(raw))
	if err != nil {
		t.Fatal(err)
	}
	if len(items) != 1 || items[0].ID.String() != "12" {
		t.Fatalf("unexpected items: %+v", items)
	}
	if items[0].ShareWith == nil || *items[0].ShareWith != "admin" {
		t.Fatalf("share_with = %v", items[0].ShareWith)
	}
}

func TestMapOCSShareRecordOwnerAndCreatedAt(t *testing.T) {
	with := "user@example.com"
	item := ocsShareRecord{
		ID:                   "3",
		ShareType:            3,
		Permissions:          1,
		URL:                  "http://localhost/s/abc",
		ShareWith:            &with,
		DisplayNameOwner:     "Alice",
		DisplayNameFileOwner: "Alice",
		Stime:                1780572446,
		Label:                "internal",
		ItemType:             "file",
	}
	info := mapOCSShareRecord(item, "/file.txt")
	if info.AccessMode != "internal" {
		t.Fatalf("access_mode = %q", info.AccessMode)
	}
	if info.OwnerDisplayName != "Alice" {
		t.Fatalf("owner = %q", info.OwnerDisplayName)
	}
	if !strings.Contains(info.CreatedAt, "2026") {
		t.Fatalf("created_at = %q", info.CreatedAt)
	}
}
