package admin

import "fmt"

func ValidPostStatus(status PostStatus) bool {
	switch status {
	case PostStatusDraft, PostStatusPublished, PostStatusArchived, PostStatusDeleted:
		return true
	default:
		return false
	}
}

func IsPublicPostStatus(status PostStatus) bool {
	return status == PostStatusPublished || status == PostStatusArchived
}

func ValidVersionReason(reason VersionReason) bool {
	switch reason {
	case VersionReasonSave, VersionReasonPublish, VersionReasonUnpublish, VersionReasonArchive, VersionReasonRestore, VersionReasonImport, VersionReasonRollback:
		return true
	default:
		return false
	}
}

func ValidBuildJobStatus(status BuildJobStatus) bool {
	switch status {
	case BuildJobStatusQueued, BuildJobStatusRunning, BuildJobStatusSuccess, BuildJobStatusFailed, BuildJobStatusCancelled:
		return true
	default:
		return false
	}
}

func ValidBuildJobTrigger(trigger BuildJobTrigger) bool {
	switch trigger {
	case BuildJobTriggerPublish, BuildJobTriggerManual, BuildJobTriggerImport, BuildJobTriggerSync:
		return true
	default:
		return false
	}
}

func CanTransitionPostStatus(from PostStatus, to PostStatus) bool {
	if !ValidPostStatus(from) || !ValidPostStatus(to) {
		return false
	}
	if from == to {
		return true
	}

	switch from {
	case PostStatusDraft:
		return to == PostStatusPublished || to == PostStatusArchived || to == PostStatusDeleted
	case PostStatusPublished:
		return to == PostStatusDraft || to == PostStatusArchived || to == PostStatusDeleted
	case PostStatusArchived:
		return to == PostStatusDraft || to == PostStatusPublished || to == PostStatusDeleted
	case PostStatusDeleted:
		return to == PostStatusDraft
	default:
		return false
	}
}

func ValidatePostStatusTransition(from PostStatus, to PostStatus) error {
	if CanTransitionPostStatus(from, to) {
		return nil
	}
	return fmt.Errorf("invalid post status transition: %s -> %s", from, to)
}
