package copy_mission

import (
	"context"
	"strings"

	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

type GetCopyMissionScanScheduleLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetCopyMissionScanScheduleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCopyMissionScanScheduleLogic {
	return &GetCopyMissionScanScheduleLogic{ctx: ctx, svcCtx: svcCtx}
}

func (l *GetCopyMissionScanScheduleLogic) GetCopyMissionScanSchedule(
	req *types.CopyMissionPath,
) (*types.CopyMissionScanScheduleData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	personaID := strings.TrimSpace(req.PersonaID)
	missionID := strings.TrimSpace(req.ID)
	if _, err := l.svcCtx.Persona.Get(l.ctx, tenantID, uid, personaID); err != nil {
		return nil, err
	}
	if _, err := l.svcCtx.CopyMission.Get(l.ctx, tenantID, uid, personaID, missionID); err != nil {
		return nil, err
	}
	schedule, err := findCopyMissionScanSchedule(l.ctx, l.svcCtx, missionID)
	if err != nil {
		return nil, err
	}
	return toCopyMissionScanScheduleData(schedule, personaID, missionID), nil
}
