package entity

import (
	"time"

	coreuser "github.com/tidbcloud/aisupport/packages/sdk-go/contracts/entity/core-server/user/v1"
)

type (
	IncidentStatus string
	IncidentTier   string

	LogItemType string
)

const (
	IncidentStatusRunning  IncidentStatus = "running"
	IncidentStatusFinished IncidentStatus = "completed"

	PostMortemToDoActionImportStatusIdle      string = ""
	PostMortemToDoActionImportStatusImporting string = "importing"
	PostMortemToDoActionImportStatusSucceeded string = "succeeded"
	PostMortemToDoActionImportStatusFailed    string = "failed"

	IncidentTierT0 IncidentTier = "t0"
	IncidentTierT1 IncidentTier = "t1"
	IncidentTierT2 IncidentTier = "t2"
	IncidentTierT3 IncidentTier = "t3"
	IncidentTierT4 IncidentTier = "t4"

	LogItemTypeText       LogItemType = "text"
	LogItemTypePostMortem LogItemType = "postmortem"
	LogItemTypeGeneral    LogItemType = "general"
	LogItemTypeMilestone  LogItemType = "milestone"
)

type Incident struct {
	Id   uint64 `json:"id"`
	Name string `json:"name"`

	CreatedAt *time.Time `json:"created_at"`

	Status IncidentStatus `json:"status"`
	Tier   IncidentTier   `json:"tier"`

	Components string `json:"components"`
	BizRegions string `json:"biz_regions"`

	Oncallers []*coreuser.User `json:"oncallers"`
	Leaders   []*coreuser.User `json:"leaders"`
	FBOwners  []*coreuser.User `json:"fbowners"`

	FeishuUrl      string                  `json:"feishu_url"`
	JiraIssues     string                  `json:"jira_issues"`
	JiraIssueLinks []IncidentJiraIssueLink `json:"jira_issue_links"`

	Description          string     `json:"description"`
	IncidentStartTime    *time.Time `json:"incident_start_time"`
	IncidentResolvedTime *time.Time `json:"incident_resolve_time"`
	WarRoomID            *uint64    `json:"war_room_id"`

	LogItems             []IncidentLogItem `json:"log_items"`
	FeishuChannelMessage string            `json:"feishu_channel_msg"`

	PostmortemAvailable              bool   `json:"postmortem_available"`
	PostMortemToDoActionImportStatus string `json:"postmortem_todo_action_import_status"`
} //	@name	entity.Incident

type IncidentJiraIssueLink struct {
	Key string `json:"key"`
	URL string `json:"url"`
} // @name entity.IncidentJiraIssueLink

type IncidentDigest struct {
	Id   uint64 `json:"id"`
	Name string `json:"name"`

	CreatedAt *time.Time `json:"created_at"`

	Status IncidentStatus `json:"status"`
	Tier   IncidentTier   `json:"tier"`

	Components string `json:"components"`
	BizRegions string `json:"biz_regions"`

	FeishuUrl string `json:"feishu_url"`

	LatestEvent IncidentLog `json:"latest_event"`
} //	@name	entity.IncidentDigest

type IncidentLog struct {
	Id         uint64            `json:"id"`
	IncidentId uint64            `json:"incident_id"`
	Items      []IncidentLogItem `json:"items"`
} //	@name	entity.IncidentLog

type IncidentLogItem struct {
	Id            uint64      `json:"id"`
	IncidentId    uint64      `json:"incident_id"`
	Description   string      `json:"description"`
	ItemType      LogItemType `json:"item_type"`
	MilestoneType string      `json:"milestone_type"`
	From          string      `json:"from"`
	PostMortemUrl string      `json:"postmortem_url"`
	Status        string      `json:"status"`
	EventTime     *time.Time  `json:"event_time"`
	CreatedAt     *time.Time  `json:"created_at"` // Will be deprecated next release
} //	@name	entity.IncidentLogItem

type IncidentPostMortem struct {
	Id                    uint64                            `json:"id"`
	IncidentId            uint64                            `json:"incident_id"`
	CreatedAt             *time.Time                        `json:"created_at"`
	ReviewedAt            *time.Time                        `json:"reviewed_at"`
	InvolvedEngineers     []*coreuser.User                  `json:"involved_engineers"`
	IncidentStartTime     *time.Time                        `json:"incident_start_time"`
	IncidentEndTime       *time.Time                        `json:"incident_end_time"`
	ImpactedTenants       string                            `json:"involved_tenants"`
	TechnicalImpactLevel  string                            `json:"technical_impact_level"`
	MinutesToResolved     uint64                            `json:"minutes_to_resolved"`
	CustomerImpactMinutes uint64                            `json:"customer_impact_minutes"`
	Summary               string                            `json:"summary"`
	Impact                string                            `json:"impact"`
	Timeline              []*IncidentPostMortemTimelineItem `json:"timeline"`
	MinutesToDetect       uint64                            `json:"minutes_to_detect"`
	Resolution            string                            `json:"resolution"`
	RootCauses            string                            `json:"root_causes"`
	Background            string                            `json:"background"`
	BugMechanism          string                            `json:"bug_mechanism"`
	WhatWorked            string                            `json:"what_worked"`
	WhatDidNotWork        string                            `json:"what_did_not_work"`
	WhereWeWereLucky      string                            `json:"where_we_were_lucky"`
	TopIssues             string                            `json:"top_issues"`
	ActionItems           []*IncidentPostMortemToDoAction   `json:"action_items"`
} //	@name	entity.IncidentPostMortem

type IncidentPostMortemTimelineItem struct {
	Id           uint64     `json:"id"`
	PostMortemId uint64     `json:"postmortem_id"`
	HappenedAt   *time.Time `json:"happened_at"`
	ItemType     string     `json:"item_type"`
	Description  string     `json:"description"`
} //	@name	entity.IncidentPostMortemTimelineItem

type IncidentPostMortemToDoAction struct {
	Id              uint64           `json:"id"`
	IncidentId      *uint64          `json:"incident_id"`
	PostMortemId    *uint64          `json:"postmortem_id"`
	Persons         []*coreuser.User `json:"persons"`
	Deadline        *time.Time       `json:"deadline"`
	Title           *string          `json:"title"`
	Type            *string          `json:"type"`
	Priority        *string          `json:"priority"`
	Description     *string          `json:"description"`
	Status          *string          `json:"status"` // Open or InProgress or Done
	LinkedJiraIssue *string          `json:"linked_jira_issue"`
	TCOCPCreatedAt  *time.Time       `json:"tcocp_created_at"`
	FeishuTaskId    *string          `json:"feishu_task_id"`
} //	@name	entity.IncidentPostMortemToDoAction

type MessageReply struct {
	Time    time.Time
	Content string
}

type FeishuTask struct {
	Title        string     `json:"title"`
	Description  string     `json:"description"`
	DueTime      *time.Time `json:"due_time"`
	IncidentUrl  string     `json:"incident_url"`
	Executors    []string   `json:"executors"`
	Followers    []string   `json:"followers"`
	CompleteTime *string    `json:"complete_time"`
}
