package cvt

import (
	"strings"

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

type e2m struct{}

func (cvt e2m) IncidentPostMortem(e *entity.IncidentPostMortem) *models.IncidentPostMortem {
	model := &models.IncidentPostMortem{}
	model.IncidentId = e.IncidentId
	model.ReviewedAt = e.ReviewedAt
	model.IncidentStartTime = e.IncidentStartTime
	model.IncidentEndTime = e.IncidentEndTime
	model.ImpactedTenants = e.ImpactedTenants
	model.TechnicalImpactLevel = e.TechnicalImpactLevel
	model.MinutesToResolved = e.MinutesToResolved
	model.CustomerImpactMinutes = e.CustomerImpactMinutes
	model.Summary = e.Summary
	model.Impact = e.Impact
	model.MinutesToDetect = e.MinutesToDetect
	model.Resolution = e.Resolution
	model.RootCauses = e.RootCauses
	model.Background = e.Background
	model.BugMechanism = e.BugMechanism
	model.WhatWorked = e.WhatWorked
	model.WhatDidNotWork = e.WhatDidNotWork
	model.WhereWeWereLucky = e.WhereWeWereLucky
	model.TopIssues = e.TopIssues
	var engineers []string
	for _, p := range e.InvolvedEngineers {
		engineers = append(engineers, p.Email)
	}
	model.InvolvedEngineers = strings.Join(engineers, " ")

	return model
}

func (cvt e2m) IncidentPostMortemTimelineItem(e *entity.IncidentPostMortemTimelineItem) *models.IncidentPostMortemTimelineItem {
	model := &models.IncidentPostMortemTimelineItem{}
	model.Id = e.Id
	model.PostMortemId = e.PostMortemId
	model.HappenedAt = e.HappenedAt
	model.ItemType = e.ItemType
	model.Description = e.Description
	return model
}

func (cvt e2m) IncidentPostMortemToDoAction(e *entity.IncidentPostMortemToDoAction) *models.IncidentPostMortemToDoAction {
	model := &models.IncidentPostMortemToDoAction{}
	model.IncidentId = e.IncidentId
	model.PostMortemId = e.PostMortemId
	model.Deadline = e.Deadline
	model.Status = e.Status
	model.Description = e.Description
	model.Title = e.Title
	model.Type = e.Type
	model.Priority = e.Priority
	model.LinkedJiraIssue = e.LinkedJiraIssue
	model.TCOCPCreatedAt = e.TCOCPCreatedAt
	model.FeishuTaskId = e.FeishuTaskId
	var persons []string
	for _, p := range e.Persons {
		persons = append(persons, p.Email)
	}
	personEmailString := strings.Join(persons, " ")
	model.Persons = &personEmailString

	return model
}

func (cvt e2m) IncidentLogItem(e *entity.IncidentLogItem) *models.IncidentLogItem {
	model := &models.IncidentLogItem{}
	model.IncidentId = e.IncidentId
	model.LogItemType = models.IncidentLogItemTp(e.ItemType)
	model.Content = e.Description
	model.PostMortemLink = e.PostMortemUrl
	model.EventTime = e.EventTime
	model.Status = e.Status
	model.MilestoneType = e.MilestoneType
	model.From = e.From

	return model
}
