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/skill.sql
var skillSQL string

func TestSkillGroups(t *testing.T) {
	db := testdb(t.Context(), "file:TestSkillGroups?mode=memory&cache=shared", skillSQL)
	got, err := mdb.SkillGroups(t.Context(), db)
	if err != nil {
		t.Error(err)
	}
	want := []horse.SkillGroup{
		{ID: 1035, Skill1: 10351},
		{ID: 10001, Skill1: 100011, Skill2: 900011},
		{ID: 10035, Skill1: 100351, Skill2: 900351},
		{ID: 11024, Skill1: 110241, Skill2: 910241},
		{ID: 20001, Skill1: 200012, Skill2: 200011, Skill3: 200014, SkillBad: 200013},
		{ID: 20002, Skill1: 200022, Skill2: 200021, SkillBad: 200023},
		{ID: 20036, Skill1: 200362, Skill2: 200361},
		{ID: 20083, Skill1: 200831},
		{ID: 20180, SkillBad: 201801},
		{ID: 30001, Skill1: 300011},
		{ID: 90001, Skill1: 100011, Skill2: 900011},
		{ID: 90035, Skill1: 100351, Skill2: 900351},
		{ID: 91024, Skill1: 110241, Skill2: 910241},
	}
	if diff := cmp.Diff(want, got); diff != "" {
		t.Errorf("wrong skill groups (+got/-want):\n%s", diff)
	}
}

func TestSkills(t *testing.T) {
	db := testdb(t.Context(), "file:TestSkills?mode=memory&cache=shared", skillSQL)
	got, err := mdb.Skills(t.Context(), db)
	if err != nil {
		t.Error(err)
	}
	want := []horse.Skill{
		{
			ID:          10351,
			Name:        "V Is for Victory!",
			Description: "Moderately increase velocity with winning ambition when positioned toward the front on the final straight after engaging in a challenge on the final corner or later.",
			Group:       1035,
			Rarity:      3,
			GroupRate:   1,
			GradeValue:  240,
			WitCheck:    false,
			Activations: []horse.Activation{
				{
					Precondition: "is_finalcorner==1&blocked_side_continuetime>=2",
					Condition:    "is_finalcorner==1&corner==0&order<=5",
					Duration:     50000,
					DurScale:     1,
					Cooldown:     5000000,
					Abilities: []horse.Ability{
						{
							Type:        27,
							ValueUsage:  1,
							Value:       2500,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			UniqueOwner: "[Get to Winning!] Winning Ticket",
			Tags:        []uint16{401},
			SPCost:      0,
			IconID:      20013,
		},
		{
			ID:          100011,
			Name:        "Shooting Star",
			Description: "Ride the momentum to increase velocity and very slightly increase acceleration after passing another runner toward the front late-race.",
			Group:       10001,
			Rarity:      5,
			GroupRate:   1,
			GradeValue:  340,
			WitCheck:    false,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "phase>=2&order>=1&order_rate<=50&change_order_onetime<0",
					Duration:     50000,
					DurScale:     1,
					Cooldown:     5000000,
					Abilities: []horse.Ability{
						{
							Type:        27,
							ValueUsage:  1,
							Value:       3500,
							Target:      1,
							TargetValue: 0,
						},
						{
							Type:        31,
							ValueUsage:  1,
							Value:       1000,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			UniqueOwner: "[Special Dreamer] Special Week",
			Tags:        []uint16{401, 403},
			SPCost:      0,
			IconID:      20013,
		},
		{
			ID:          100351,
			Name:        "Our Ticket to Win!",
			Description: "Increase velocity with winning ambition when positioned toward the front on the final straight after engaging in a challenge on the final corner or later.",
			Group:       10035,
			Rarity:      4,
			GroupRate:   1,
			GradeValue:  340,
			WitCheck:    false,
			Activations: []horse.Activation{
				{
					Precondition: "is_finalcorner==1&blocked_side_continuetime>=2",
					Condition:    "is_finalcorner==1&corner==0&order<=5",
					Duration:     50000,
					DurScale:     1,
					Cooldown:     5000000,
					Abilities: []horse.Ability{
						{
							Type:        27,
							ValueUsage:  1,
							Value:       3500,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			UniqueOwner: "[Get to Winning!] Winning Ticket",
			Tags:        []uint16{401},
			SPCost:      0,
			IconID:      20013,
		},
		{
			ID:          110241,
			Name:        "Flowery☆Maneuver",
			Description: "Increase velocity when passing another runner toward the front on the final corner. If passing toward the back, increase acceleration instead.",
			Group:       11024,
			Rarity:      5,
			GroupRate:   1,
			GradeValue:  340,
			WitCheck:    false,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "is_finalcorner==1&corner!=0&order_rate<=40&change_order_onetime<0",
					Duration:     50000,
					DurScale:     1,
					Cooldown:     5000000,
					Abilities: []horse.Ability{
						{
							Type:        27,
							ValueUsage:  1,
							Value:       3500,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
				{
					Precondition: "",
					Condition:    "is_finalcorner==1&corner!=0&order_rate>=50&order_rate<=80&change_order_onetime<0",
					Duration:     40000,
					DurScale:     1,
					Cooldown:     5000000,
					Abilities: []horse.Ability{
						{
							Type:        31,
							ValueUsage:  1,
							Value:       4000,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			UniqueOwner: "[Sunlight Bouquet] Mayano Top Gun",
			Tags:        []uint16{401, 403},
			SPCost:      0,
			IconID:      20013,
		},
		{
			ID:          200011,
			Name:        "Right-Handed ◎",
			Description: "Increase performance on right-handed tracks.",
			Group:       20001,
			Rarity:      1,
			GroupRate:   2,
			GradeValue:  174,
			WitCheck:    false,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "rotation==1",
					Duration:     -1,
					DurScale:     1,
					Cooldown:     0,
					Abilities: []horse.Ability{
						{
							Type:        1,
							ValueUsage:  1,
							Value:       600000,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			Tags:   []uint16{401},
			SPCost: 110,
			IconID: 10011,
		},
		{
			ID:          200012,
			Name:        "Right-Handed ○",
			Description: "Moderately increase performance on right-handed tracks.",
			Group:       20001,
			Rarity:      1,
			GroupRate:   1,
			GradeValue:  129,
			WitCheck:    false,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "rotation==1",
					Duration:     -1,
					DurScale:     1,
					Cooldown:     0,
					Abilities: []horse.Ability{
						{
							Type:        1,
							ValueUsage:  1,
							Value:       400000,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			Tags:   []uint16{401},
			SPCost: 90,
			IconID: 10011,
		},
		{
			ID:          200013,
			Name:        "Right-Handed ×",
			Description: "Moderately decrease performance on right-handed tracks.",
			Group:       20001,
			Rarity:      1,
			GroupRate:   -1,
			GradeValue:  -129,
			WitCheck:    false,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "rotation==1",
					Duration:     -1,
					DurScale:     1,
					Cooldown:     0,
					Abilities: []horse.Ability{
						{
							Type:        1,
							ValueUsage:  1,
							Value:       -400000,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			Tags:   []uint16{401},
			SPCost: 50,
			IconID: 10014,
		},
		{
			ID:          200014,
			Name:        "Right-Handed Demon",
			Description: "Increase proficiency in right-handed tracks, increasing Speed and Power.",
			Group:       20001,
			Rarity:      2,
			GroupRate:   3,
			GradeValue:  461,
			WitCheck:    false,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "rotation==1",
					Duration:     -1,
					DurScale:     1,
					Cooldown:     0,
					Abilities: []horse.Ability{
						{
							Type:        1,
							ValueUsage:  1,
							Value:       600000,
							Target:      1,
							TargetValue: 0,
						},
						{
							Type:        3,
							ValueUsage:  1,
							Value:       600000,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			Tags:   []uint16{401, 403},
			SPCost: 130,
			IconID: 10012,
		},
		{
			ID:          200021,
			Name:        "Left-Handed ◎",
			Description: "Increase performance on left-handed tracks.",
			Group:       20002,
			Rarity:      1,
			GroupRate:   2,
			GradeValue:  174,
			WitCheck:    false,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "rotation==2",
					Duration:     -1,
					DurScale:     1,
					Cooldown:     0,
					Abilities: []horse.Ability{
						{
							Type:        1,
							ValueUsage:  1,
							Value:       600000,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			Tags:   []uint16{401},
			SPCost: 110,
			IconID: 10011,
		},
		{
			ID:          200022,
			Name:        "Left-Handed ○",
			Description: "Moderately increase performance on left-handed tracks.",
			Group:       20002,
			Rarity:      1,
			GroupRate:   1,
			GradeValue:  129,
			WitCheck:    false,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "rotation==2",
					Duration:     -1,
					DurScale:     1,
					Cooldown:     0,
					Abilities: []horse.Ability{
						{
							Type:        1,
							ValueUsage:  1,
							Value:       400000,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			Tags:   []uint16{401},
			SPCost: 90,
			IconID: 10011,
		},
		{
			ID:          200023,
			Name:        "Left-Handed ×",
			Description: "Moderately decrease performance on left-handed tracks.",
			Group:       20002,
			Rarity:      1,
			GroupRate:   -1,
			GradeValue:  -129,
			WitCheck:    false,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "rotation==2",
					Duration:     -1,
					DurScale:     1,
					Cooldown:     0,
					Abilities: []horse.Ability{
						{
							Type:        1,
							ValueUsage:  1,
							Value:       -400000,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			Tags:   []uint16{401},
			SPCost: 50,
			IconID: 10014,
		},
		{
			ID:          200361,
			Name:        "Beeline Burst",
			Description: "Increase velocity on a straight.",
			Group:       20036,
			Rarity:      2,
			GroupRate:   2,
			GradeValue:  508,
			WitCheck:    true,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "straight_random==1",
					Duration:     24000,
					DurScale:     1,
					Cooldown:     300000,
					Abilities: []horse.Ability{
						{
							Type:        27,
							ValueUsage:  1,
							Value:       3500,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			Tags:   []uint16{401},
			SPCost: 170,
			IconID: 20012,
		},
		{
			ID:          200362,
			Name:        "Straightaway Adept",
			Description: "Slightly increase velocity on a straight.",
			Group:       20036,
			Rarity:      1,
			GroupRate:   1,
			GradeValue:  217,
			WitCheck:    true,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "straight_random==1",
					Duration:     24000,
					DurScale:     1,
					Cooldown:     300000,
					Abilities: []horse.Ability{
						{
							Type:        27,
							ValueUsage:  1,
							Value:       1500,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			Tags:   []uint16{401},
			SPCost: 170,
			IconID: 20011,
		},
		{
			ID:          200831,
			Name:        "Subdued Front Runners",
			Description: "Slightly increase fatigue for front runners early-race.",
			Group:       20083,
			Rarity:      1,
			GroupRate:   1,
			GradeValue:  217,
			WitCheck:    true,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "running_style_count_nige_otherself>=1&phase_random==0&accumulatetime>=5",
					Duration:     0,
					DurScale:     1,
					Cooldown:     5000000,
					Abilities: []horse.Ability{
						{
							Type:        9,
							ValueUsage:  1,
							Value:       -100,
							Target:      18,
							TargetValue: 1,
						},
					},
				},
			},
			Tags:   []uint16{301, 406},
			SPCost: 130,
			IconID: 30051,
		},
		{
			ID:          201801,
			Name:        "♡ 3D Nail Art",
			Description: "Moderately decrease performance on firm ground.",
			Group:       20180,
			Rarity:      1,
			GroupRate:   -1,
			GradeValue:  -129,
			WitCheck:    false,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "ground_condition==1",
					Duration:     -1,
					DurScale:     1,
					Cooldown:     0,
					Abilities: []horse.Ability{
						{
							Type:        1,
							ValueUsage:  1,
							Value:       -400000,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			Tags:   []uint16{401},
			SPCost: 50,
			IconID: 10014,
		},
		{
			ID:          300011,
			Name:        "Unquenched Thirst",
			Description: "Moderately increase performance with the desire to race.",
			Group:       30001,
			Rarity:      1,
			GroupRate:   1,
			GradeValue:  0,
			WitCheck:    false,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "track_id==10008",
					Duration:     -1,
					DurScale:     1,
					Cooldown:     0,
					Abilities: []horse.Ability{
						{
							Type:        2,
							ValueUsage:  1,
							Value:       400000,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			Tags:   []uint16{402},
			SPCost: 0,
			IconID: 10021,
		},
		{
			ID:          900011,
			Name:        "Shooting Star",
			Description: "Slightly increase velocity and very minimally increase acceleration after passing another runner toward the front late-race.",
			Group:       10001,
			Rarity:      1,
			GroupRate:   2,
			GradeValue:  180,
			WitCheck:    true,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "phase>=2&order>=1&order_rate<=50&change_order_onetime<0",
					Duration:     30000,
					DurScale:     1,
					Cooldown:     5000000,
					Abilities: []horse.Ability{
						{
							Type:        27,
							ValueUsage:  1,
							Value:       1500,
							Target:      1,
							TargetValue: 0,
						},
						{
							Type:        31,
							ValueUsage:  1,
							Value:       500,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			UniqueOwner: "[Special Dreamer] Special Week",
			Tags:        []uint16{401, 403},
			SPCost:      200,
			IconID:      20011,
		},
		{
			ID:          900351,
			Name:        "Our Ticket to Win!",
			Description: "Slightly increase velocity when positioned toward the front on the final straight after engaging in a challenge on the final corner or later.",
			Group:       10035,
			Rarity:      1,
			GroupRate:   2,
			GradeValue:  180,
			WitCheck:    true,
			Activations: []horse.Activation{
				{
					Precondition: "is_finalcorner==1&blocked_side_continuetime>=2",
					Condition:    "is_finalcorner==1&corner==0&order<=5",
					Duration:     30000,
					DurScale:     1,
					Cooldown:     5000000,
					Abilities: []horse.Ability{
						{
							Type:        27,
							ValueUsage:  1,
							Value:       1500,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			UniqueOwner: "[Get to Winning!] Winning Ticket",
			Tags:        []uint16{401},
			SPCost:      200,
			IconID:      20011,
		},
		{
			ID:          910241,
			Name:        "Flowery☆Maneuver",
			Description: "Slightly increase velocity when passing another runner toward the front on the final corner. If passing toward the back, slightly increase acceleration instead.",
			Group:       11024,
			Rarity:      1,
			GroupRate:   2,
			GradeValue:  180,
			WitCheck:    true,
			Activations: []horse.Activation{
				{
					Precondition: "",
					Condition:    "is_finalcorner==1&corner!=0&order_rate<=40&change_order_onetime<0",
					Duration:     30000,
					DurScale:     1,
					Cooldown:     5000000,
					Abilities: []horse.Ability{
						{
							Type:        27,
							ValueUsage:  1,
							Value:       1500,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
				{
					Precondition: "",
					Condition:    "is_finalcorner==1&corner!=0&order_rate>=50&order_rate<=80&change_order_onetime<0",
					Duration:     24000,
					DurScale:     1,
					Cooldown:     5000000,
					Abilities: []horse.Ability{
						{
							Type:        31,
							ValueUsage:  1,
							Value:       2000,
							Target:      1,
							TargetValue: 0,
						},
					},
				},
			},
			UniqueOwner: "[Sunlight Bouquet] Mayano Top Gun",
			Tags:        []uint16{401, 403},
			SPCost:      200,
			IconID:      20011,
		},
	}
	if diff := cmp.Diff(want, got); diff != "" {
		t.Errorf("wrong skills (+got/-want):\n%s", diff)
	}
}
