package cvt

import (
	"github.com/tidbcloud/devops/entity"
	"github.com/tidbcloud/devops/models"
)

type m2e struct{}

func (cvt m2e) IncidentPostMortem(postmortem *models.IncidentPostMortem) *entity.IncidentPostMortem {
	incidentPostMortemEntity := &entity.IncidentPostMortem{}
	incidentPostMortemEntity.Id = postmortem.GetID().Uint64()
	incidentPostMortemEntity.IncidentId = postmortem.IncidentId
	incidentPostMortemEntity.CreatedAt = &postmortem.CreatedAt
	incidentPostMortemEntity.ReviewedAt = postmortem.ReviewedAt
	incidentPostMortemEntity.IncidentStartTime = postmortem.IncidentStartTime
	incidentPostMortemEntity.IncidentEndTime = postmortem.IncidentEndTime
	incidentPostMortemEntity.ImpactedTenants = postmortem.ImpactedTenants
	incidentPostMortemEntity.TechnicalImpactLevel = postmortem.TechnicalImpactLevel
	incidentPostMortemEntity.MinutesToResolved = postmortem.MinutesToResolved
	incidentPostMortemEntity.CustomerImpactMinutes = postmortem.CustomerImpactMinutes
	incidentPostMortemEntity.Summary = postmortem.Summary
	incidentPostMortemEntity.Impact = postmortem.Impact
	incidentPostMortemEntity.MinutesToDetect = postmortem.MinutesToDetect
	incidentPostMortemEntity.Resolution = postmortem.Resolution
	incidentPostMortemEntity.RootCauses = postmortem.RootCauses
	incidentPostMortemEntity.Background = postmortem.Background
	incidentPostMortemEntity.BugMechanism = postmortem.BugMechanism
	incidentPostMortemEntity.WhatWorked = postmortem.WhatWorked
	incidentPostMortemEntity.WhatDidNotWork = postmortem.WhatDidNotWork
	incidentPostMortemEntity.WhereWeWereLucky = postmortem.WhereWeWereLucky
	incidentPostMortemEntity.TopIssues = postmortem.TopIssues
	return incidentPostMortemEntity
}

func (cvt m2e) IncidentPostMortemTimelineItem(timeline *models.IncidentPostMortemTimelineItem) *entity.IncidentPostMortemTimelineItem {
	timelineEntity := &entity.IncidentPostMortemTimelineItem{}
	timelineEntity.Id = timeline.Id
	timelineEntity.PostMortemId = timeline.PostMortemId
	timelineEntity.ItemType = timeline.ItemType
	timelineEntity.HappenedAt = timeline.HappenedAt
	timelineEntity.Description = timeline.Description
	return timelineEntity
}

func (cvt m2e) IncidentPostMortemToDoAction(action *models.IncidentPostMortemToDoAction) *entity.IncidentPostMortemToDoAction {
	actionEntity := &entity.IncidentPostMortemToDoAction{}
	actionEntity.Id = action.ID.Uint64()
	actionEntity.IncidentId = action.IncidentId
	actionEntity.PostMortemId = action.PostMortemId
	actionEntity.Description = action.Description
	actionEntity.Title = action.Title
	actionEntity.Type = action.Type
	actionEntity.Priority = action.Priority
	actionEntity.Deadline = action.Deadline
	actionEntity.Status = action.Status
	actionEntity.LinkedJiraIssue = action.LinkedJiraIssue
	actionEntity.TCOCPCreatedAt = action.TCOCPCreatedAt
	actionEntity.FeishuTaskId = action.FeishuTaskId
	return actionEntity
}

func (cvt m2e) IncidentLogItem(logItem *models.IncidentLogItem) *entity.IncidentLogItem {
	e := &entity.IncidentLogItem{}
	e.Id = logItem.ID.Uint64()
	e.IncidentId = logItem.IncidentId
	e.ItemType = entity.LogItemType(logItem.LogItemType)
	e.Status = logItem.Status
	e.EventTime = logItem.EventTime
	e.MilestoneType = logItem.MilestoneType
	e.Description = logItem.Content
	if logItem.EventTime == nil {
		e.CreatedAt = &logItem.CreatedAt
	} else {
		e.CreatedAt = logItem.EventTime
	}
	e.PostMortemUrl = logItem.PostMortemLink
	e.From = logItem.From
	return e
}
