package task

import (
	"context"
	"fmt"

	"git.huangwc.com/pig/pig-farm-controller/internal/domain/alarm"
	"git.huangwc.com/pig/pig-farm-controller/internal/domain/device"
	"git.huangwc.com/pig/pig-farm-controller/internal/domain/notify"
	"git.huangwc.com/pig/pig-farm-controller/internal/domain/plan"
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
)

const (
	CompNameDelayTask          = "DelayTask"
	CompNameReleaseFeedWeight  = "ReleaseFeedWeightTask"
	CompNameFullCollectionTask = "FullCollectionTask"
	CompNameAlarmNotification  = "AlarmNotificationTask"
)

type taskFactory struct {
	ctx context.Context

	sensorDataRepo repository.SensorDataRepository
	deviceRepo     repository.DeviceRepository
	alarmRepo      repository.AlarmRepository

	deviceService       device.Service
	notificationService notify.Service
	alarmService        alarm.AlarmService
}

func NewTaskFactory(
	ctx context.Context,
	sensorDataRepo repository.SensorDataRepository,
	deviceRepo repository.DeviceRepository,
	alarmRepo repository.AlarmRepository,
	deviceService device.Service,
	notifyService notify.Service,
	alarmService alarm.AlarmService,
) plan.TaskFactory {
	return &taskFactory{
		ctx:                 ctx,
		sensorDataRepo:      sensorDataRepo,
		deviceRepo:          deviceRepo,
		alarmRepo:           alarmRepo,
		deviceService:       deviceService,
		notificationService: notifyService,
		alarmService:        alarmService,
	}
}

func (t *taskFactory) Production(ctx context.Context, claimedLog *models.TaskExecutionLog) plan.Task {
	logger := logs.TraceLogger(ctx, t.ctx, "Production")
	baseCtx := context.Background()
	switch claimedLog.Task.Type {
	case models.TaskTypeWaiting:
		return NewDelayTask(logs.AddCompName(baseCtx, CompNameDelayTask), claimedLog)
	case models.TaskTypeReleaseFeedWeight:
		return NewReleaseFeedWeightTask(logs.AddCompName(baseCtx, CompNameReleaseFeedWeight), claimedLog, t.sensorDataRepo, t.deviceRepo, t.deviceService)
	case models.TaskTypeFullCollection:
		return NewFullCollectionTask(logs.AddCompName(baseCtx, CompNameFullCollectionTask), claimedLog, t.deviceRepo, t.deviceService)
	case models.TaskTypeAlarmNotification:
		return NewAlarmNotificationTask(logs.AddCompName(baseCtx, CompNameAlarmNotification), claimedLog, t.notificationService, t.alarmRepo)
	case models.TaskTypeDeviceThresholdCheck:
		return NewDeviceThresholdCheckTask(logs.AddCompName(baseCtx, "DeviceThresholdCheckTask"), claimedLog, t.sensorDataRepo, t.alarmService)
	case models.TaskTypeAreaCollectorThresholdCheck:
		return NewAreaThresholdCheckTask(logs.AddCompName(baseCtx, "AreaCollectorThresholdCheckTask"), claimedLog, t.sensorDataRepo, t.deviceRepo, t.alarmService)
	case models.TaskTypeNotificationRefresh:
		return NewRefreshNotificationTask(logs.AddCompName(baseCtx, "NotificationRefreshTask"), claimedLog, t.alarmService)
	default:
		// TODO 这里直接panic合适吗? 不过这个场景确实不该出现任何异常的任务类型
		logger.Panicf("不支持的任务类型: %s", claimedLog.Task.Type)
		panic("不支持的任务类型") // 显式panic防编译器报错
	}
}

// CreateTaskFromModel 实现了 TaskFactory 接口，用于从模型创建任务实例。
func (t *taskFactory) CreateTaskFromModel(ctx context.Context, taskModel *models.Task) (plan.TaskDeviceIDResolver, error) {
	// 这个方法不关心 claimedLog 的其他字段，所以可以构造一个临时的
	// 它只用于访问那些不依赖于执行日志的方法，比如 ResolveDeviceIDs
	tempLog := &models.TaskExecutionLog{Task: *taskModel}
	baseCtx := context.Background()

	switch taskModel.Type {
	case models.TaskTypeWaiting:
		return NewDelayTask(logs.AddCompName(baseCtx, CompNameDelayTask), tempLog), nil
	case models.TaskTypeReleaseFeedWeight:
		return NewReleaseFeedWeightTask(
			logs.AddCompName(baseCtx, CompNameReleaseFeedWeight),
			tempLog,
			t.sensorDataRepo,
			t.deviceRepo,
			t.deviceService,
		), nil
	case models.TaskTypeFullCollection:
		return NewFullCollectionTask(logs.AddCompName(baseCtx, CompNameFullCollectionTask), tempLog, t.deviceRepo, t.deviceService), nil
	case models.TaskTypeAlarmNotification:
		return NewAlarmNotificationTask(logs.AddCompName(baseCtx, CompNameAlarmNotification), tempLog, t.notificationService, t.alarmRepo), nil
	case models.TaskTypeDeviceThresholdCheck:
		return NewDeviceThresholdCheckTask(logs.AddCompName(baseCtx, "DeviceThresholdCheckTask"), tempLog, t.sensorDataRepo, t.alarmService), nil
	case models.TaskTypeAreaCollectorThresholdCheck:
		return NewAreaThresholdCheckTask(logs.AddCompName(baseCtx, "AreaCollectorThresholdCheckTask"), tempLog, t.sensorDataRepo, t.deviceRepo, t.alarmService), nil
	case models.TaskTypeNotificationRefresh:
		return NewRefreshNotificationTask(logs.AddCompName(baseCtx, "NotificationRefreshTask"), tempLog, t.alarmService), nil
	default:
		return nil, fmt.Errorf("不支持为类型 '%s' 的任务创建模型实例", taskModel.Type)
	}
}
