package mdb_test

import (
	_ "embed"
	"testing"

	"git.sunturtle.xyz/zephyr/horse"
	"git.sunturtle.xyz/zephyr/horse/mdb"
	"github.com/google/go-cmp/cmp"
)

//go:embed testdata/character.sql
var characterSQL string

func TestCharacters(t *testing.T) {
	db := testdb(t.Context(), "file:TestCharacters?mode=memory&cache=shared", characterSQL)
	got, err := mdb.Characters(t.Context(), db)
	if err != nil {
		t.Error(err)
	}
	want := []horse.Character{
		{ID: 1001, Name: "Special Week"},
		{ID: 1002, Name: "Silence Suzuka"},
		{ID: 1078, Name: "Yamanin Zephyr"},
		{ID: 2001, Name: "Happy Meek"},
		{ID: 9001, Name: "Tazuna Hayakawa"},
	}
	if diff := cmp.Diff(want, got); diff != "" {
		t.Errorf("wrong characters (+got/-want):\n%s", diff)
	}
}

func TestAffinitySummary(t *testing.T) {
	db := testdb(t.Context(), "file:TestAffinitySummary?mode=memory&cache=shared", characterSQL)
	got, err := mdb.AffinitySummary(t.Context(), db)
	if err != nil {
		t.Error(err)
	}
	want := []horse.AffinityRelation{
		{IDA: 1001, IDB: 1002, Affinity: 23},
	}
	if diff := cmp.Diff(want, got); diff != "" {
		t.Errorf("wrong affinity (+got/-want):\n%s", diff)
	}
}

func TestAffinityDetail(t *testing.T) {
	db := testdb(t.Context(), "file:TestAffinityDetail?mode=memory&cache=shared", characterSQL)
	got, err := mdb.AffinityDetail(t.Context(), db)
	if err != nil {
		t.Error(err)
	}
	want := []horse.AffinityDetail{
		{CharaID: 1001, Relation: 103, Affinity: 2},
		{CharaID: 1001, Relation: 202, Affinity: 2},
		{CharaID: 1001, Relation: 301, Affinity: 2},
		{CharaID: 1001, Relation: 401, Affinity: 2},
		{CharaID: 1001, Relation: 502, Affinity: 2},
		{CharaID: 1001, Relation: 510, Affinity: 2},
		{CharaID: 1001, Relation: 2001, Affinity: 1},
		{CharaID: 1001, Relation: 2102, Affinity: 1},
		{CharaID: 1001, Relation: 2301, Affinity: 1},
		{CharaID: 1001, Relation: 2406, Affinity: 1},
		{CharaID: 1001, Relation: 2506, Affinity: 1},
		{CharaID: 1001, Relation: 2510, Affinity: 1},
		{CharaID: 1001, Relation: 2516, Affinity: 1},
		{CharaID: 1001, Relation: 2519, Affinity: 1},
		{CharaID: 1001, Relation: 2601, Affinity: 1},
		{CharaID: 1001, Relation: 2603, Affinity: 1},
		{CharaID: 1001, Relation: 2811, Affinity: 1},
		{CharaID: 1001, Relation: 2903, Affinity: 7},
		{CharaID: 1001, Relation: 3003, Affinity: 7},
		{CharaID: 1001, Relation: 3004, Affinity: 7},
		{CharaID: 1001, Relation: 3101, Affinity: 7},
		{CharaID: 1001, Relation: 3205, Affinity: 1},
		{CharaID: 1001, Relation: 7003, Affinity: 1},
		{CharaID: 1001, Relation: 7004, Affinity: 1},
		{CharaID: 1001, Relation: 7005, Affinity: 1},
		{CharaID: 1001, Relation: 7006, Affinity: 1},
		{CharaID: 1001, Relation: 7007, Affinity: 1},
		{CharaID: 1001, Relation: 8001, Affinity: 1},
		{CharaID: 1001, Relation: 8002, Affinity: 1},
		{CharaID: 1001, Relation: 8003, Affinity: 1},
		{CharaID: 1001, Relation: 8004, Affinity: 1},
		{CharaID: 1001, Relation: 8005, Affinity: 1},
		{CharaID: 1001, Relation: 8006, Affinity: 1},
		{CharaID: 1001, Relation: 8007, Affinity: 1},
		{CharaID: 1001, Relation: 8008, Affinity: 1},
		{CharaID: 1001, Relation: 8009, Affinity: 1},
		{CharaID: 1001, Relation: 8010, Affinity: 1},
		{CharaID: 1001, Relation: 8011, Affinity: 1},
		{CharaID: 1001, Relation: 8012, Affinity: 1},
		{CharaID: 1001, Relation: 8013, Affinity: 1},
		{CharaID: 1001, Relation: 8014, Affinity: 1},
		{CharaID: 1001, Relation: 8015, Affinity: 1},
		{CharaID: 1001, Relation: 8016, Affinity: 1},
		{CharaID: 1001, Relation: 8443, Affinity: 1},
		{CharaID: 1001, Relation: 8479, Affinity: 1},
		{CharaID: 1001, Relation: 8511, Affinity: 1},
		{CharaID: 1001, Relation: 8536, Affinity: 1},
		{CharaID: 1001, Relation: 8640, Affinity: 1},
		{CharaID: 1002, Relation: 104, Affinity: 2},
		{CharaID: 1002, Relation: 202, Affinity: 2},
		{CharaID: 1002, Relation: 301, Affinity: 2},
		{CharaID: 1002, Relation: 402, Affinity: 2},
		{CharaID: 1002, Relation: 2001, Affinity: 1},
		{CharaID: 1002, Relation: 2406, Affinity: 1},
		{CharaID: 1002, Relation: 2512, Affinity: 1},
		{CharaID: 1002, Relation: 2601, Affinity: 1},
		{CharaID: 1002, Relation: 2603, Affinity: 1},
		{CharaID: 1002, Relation: 2810, Affinity: 1},
		{CharaID: 1002, Relation: 2901, Affinity: 7},
		{CharaID: 1002, Relation: 3003, Affinity: 7},
		{CharaID: 1002, Relation: 3101, Affinity: 7},
		{CharaID: 1002, Relation: 3205, Affinity: 1},
		{CharaID: 1002, Relation: 7000, Affinity: 1},
		{CharaID: 1002, Relation: 7001, Affinity: 1},
		{CharaID: 1002, Relation: 7002, Affinity: 1},
		{CharaID: 1002, Relation: 7013, Affinity: 1},
		{CharaID: 1002, Relation: 7015, Affinity: 1},
		{CharaID: 1002, Relation: 8017, Affinity: 1},
		{CharaID: 1002, Relation: 8018, Affinity: 1},
		{CharaID: 1002, Relation: 8019, Affinity: 1},
		{CharaID: 1002, Relation: 8020, Affinity: 1},
		{CharaID: 1002, Relation: 8021, Affinity: 1},
		{CharaID: 1002, Relation: 8022, Affinity: 1},
		{CharaID: 1002, Relation: 8023, Affinity: 1},
		{CharaID: 1002, Relation: 8024, Affinity: 1},
		{CharaID: 1002, Relation: 8025, Affinity: 1},
		{CharaID: 1002, Relation: 8026, Affinity: 1},
		{CharaID: 1002, Relation: 8027, Affinity: 1},
		{CharaID: 1002, Relation: 8028, Affinity: 1},
		{CharaID: 1002, Relation: 8029, Affinity: 1},
		{CharaID: 1002, Relation: 8030, Affinity: 1},
		{CharaID: 1002, Relation: 8031, Affinity: 1},
		{CharaID: 1002, Relation: 8032, Affinity: 1},
		{CharaID: 1002, Relation: 8444, Affinity: 1},
		{CharaID: 1002, Relation: 8480, Affinity: 1},
		{CharaID: 1002, Relation: 8512, Affinity: 1},
		{CharaID: 1002, Relation: 8537, Affinity: 1},
		{CharaID: 1002, Relation: 8641, Affinity: 1},
	}
	if diff := cmp.Diff(want, got); diff != "" {
		t.Errorf("wrong affinity details (+got/-want):\n%s", diff)
	}
}

func TestUmas(t *testing.T) {
	db := testdb(t.Context(), "file:TestUmas?mode=memory&cache=shared", characterSQL)
	got, err := mdb.Umas(t.Context(), db)
	if err != nil {
		t.Error(err)
	}
	want := []horse.Uma{
		{
			ID:          100101,
			CharacterID: 1001,
			Name:        `[Special Dreamer] Special Week`,
			Variant:     `[Special Dreamer]`,
			Sprint:      2,
			Mile:        5,
			Medium:      7,
			Long:        7,
			Front:       1,
			Pace:        7,
			Late:        7,
			End:         5,
			Turf:        7,
			Dirt:        1,
			Unique:      100011,
			Skill1:      200512,
			Skill2:      201352,
			Skill3:      200732,
			SkillPL2:    200162,
			SkillPL3:    201351,
			SkillPL4:    200612,
			SkillPL5:    200511,
		},
		{
			ID:          100102,
			CharacterID: 1001,
			Name:        `[Hopp'n♪Happy Heart] Special Week`,
			Variant:     `[Hopp'n♪Happy Heart]`,
			Sprint:      2,
			Mile:        5,
			Medium:      7,
			Long:        7,
			Front:       1,
			Pace:        7,
			Late:        7,
			End:         5,
			Turf:        7,
			Dirt:        1,
			Unique:      110011,
			Skill1:      200462,
			Skill2:      200592,
			Skill3:      201132,
			SkillPL2:    200212,
			SkillPL3:    200591,
			SkillPL4:    201611,
			SkillPL5:    200461,
		},
		{
			ID:          100201,
			CharacterID: 1002,
			Name:        `[Innocent Silence] Silence Suzuka`,
			Variant:     `[Innocent Silence]`,
			Sprint:      4,
			Mile:        7,
			Medium:      7,
			Long:        3,
			Front:       7,
			Pace:        5,
			Late:        3,
			End:         1,
			Turf:        7,
			Dirt:        1,
			Unique:      100021,
			Skill1:      200432,
			Skill2:      200552,
			Skill3:      200712,
			SkillPL2:    200022,
			SkillPL3:    200431,
			SkillPL4:    200542,
			SkillPL5:    200551,
		},
	}
	if diff := cmp.Diff(want, got); diff != "" {
		t.Errorf("wrong umas (+got/-want):\n%s", diff)
	}
}

func TestConversations(t *testing.T) {
	db := testdb(t.Context(), "file:TestConversations?mode=memory&cache=shared", characterSQL)
	got, err := mdb.Conversations(t.Context(), db)
	if err != nil {
		t.Error(err)
	}
	want := []horse.Conversation{
		{
			CharacterID: 1001,
			Number:      1,
			Location:    410,
			Chara1:      1001,
		},
		{
			CharacterID:   1001,
			Number:        2,
			Location:      510,
			Chara1:        1001,
			ConditionType: 1,
		},
		{
			CharacterID:   1001,
			Number:        3,
			Location:      310,
			Chara1:        1001,
			ConditionType: 1,
		},
		{
			CharacterID:   1001,
			Number:        4,
			Location:      120,
			Chara1:        1001,
			Chara2:        1002,
			ConditionType: 2,
		},
		{
			CharacterID:   1001,
			Number:        5,
			Location:      520,
			Chara1:        1003,
			Chara2:        1001,
			ConditionType: 3,
		},
		{
			CharacterID:   1001,
			Number:        6,
			Location:      430,
			Chara1:        1001,
			Chara2:        1014,
			Chara3:        1011,
			ConditionType: 1,
		},
		{
			CharacterID: 1002,
			Number:      1,
			Location:    310,
			Chara1:      1002,
		},
		{
			CharacterID:   1002,
			Number:        2,
			Location:      210,
			Chara1:        1002,
			ConditionType: 1,
		},
		{
			CharacterID:   1002,
			Number:        3,
			Location:      110,
			Chara1:        1002,
			ConditionType: 1,
		},
		{
			CharacterID:   1002,
			Number:        4,
			Location:      520,
			Chara1:        1010,
			Chara2:        1002,
			ConditionType: 3,
		},
		{
			CharacterID:   1002,
			Number:        5,
			Location:      220,
			Chara1:        1002,
			Chara2:        1018,
			ConditionType: 2,
		},
	}
	if diff := cmp.Diff(want, got); diff != "" {
		t.Errorf("wrong conversations (+got/-want):\n%s", diff)
	}
}
