using System.Collections.Generic;
using AGVNavigationCore.Models;

namespace AGVNavigationCore.PathFinding.Validation
{
    /// <summary>
    /// 경로 검증 결과 (되돌아가기 패턴 검증 포함)
    /// </summary>
    public class PathValidationResult
    {
        /// <summary>
        /// 경로 검증이 필요한지 여부
        /// </summary>
        public bool IsValidationRequired { get; set; }

        /// <summary>
        /// 경로 검증 통과 여부
        /// </summary>
        public bool IsValid { get; set; }

        /// <summary>
        /// 검증된 경로
        /// </summary>
        public List<string> ValidatedPath { get; set; }

        /// <summary>
        /// 검출된 되돌아가기 패턴 목록 (A → B → A 형태)
        /// </summary>
        public List<BacktrackingPattern> BacktrackingPatterns { get; set; }

        /// <summary>
        /// 갈림길 노드 목록
        /// </summary>
        public List<string> JunctionNodes { get; set; }

        /// <summary>
        /// 시작 노드 ID
        /// </summary>
        public string StartNodeId { get; set; }

        /// <summary>
        /// 목표 노드 ID
        /// </summary>
        public string TargetNodeId { get; set; }

        /// <summary>
        /// 갈림길 노드 ID (방향 전환용)
        /// </summary>
        public string JunctionNodeId { get; set; }

        /// <summary>
        /// 검증 오류 메시지 (실패시)
        /// </summary>
        public string ValidationError { get; set; }

        /// <summary>
        /// 기본 생성자
        /// </summary>
        public PathValidationResult()
        {
            IsValidationRequired = false;
            IsValid = true;
            ValidatedPath = new List<string>();
            BacktrackingPatterns = new List<BacktrackingPattern>();
            JunctionNodes = new List<string>();
            StartNodeId = string.Empty;
            TargetNodeId = string.Empty;
            JunctionNodeId = string.Empty;
            ValidationError = string.Empty;
        }

        /// <summary>
        /// 검증 불필요한 경우 생성
        /// </summary>
        public static PathValidationResult CreateNotRequired()
        {
            return new PathValidationResult
            {
                IsValidationRequired = false,
                IsValid = true
            };
        }

        /// <summary>
        /// 검증 성공 결과 생성
        /// </summary>
        public static PathValidationResult CreateValid(List<string> path, string startNodeId, string targetNodeId, string junctionNodeId = "")
        {
            return new PathValidationResult
            {
                IsValidationRequired = true,
                IsValid = true,
                ValidatedPath = new List<string>(path),
                StartNodeId = startNodeId,
                TargetNodeId = targetNodeId,
                JunctionNodeId = junctionNodeId
            };
        }

        /// <summary>
        /// 검증 실패 결과 생성 (되돌아가기 패턴 검출)
        /// </summary>
        public static PathValidationResult CreateInvalidWithBacktracking(
            List<string> path,
            List<BacktrackingPattern> backtrackingPatterns,
            string startNodeId,
            string targetNodeId,
            string junctionNodeId,
            string error)
        {
            return new PathValidationResult
            {
                IsValidationRequired = true,
                IsValid = false,
                ValidatedPath = new List<string>(path),
                BacktrackingPatterns = new List<BacktrackingPattern>(backtrackingPatterns),
                StartNodeId = startNodeId,
                TargetNodeId = targetNodeId,
                JunctionNodeId = junctionNodeId,
                ValidationError = error
            };
        }

        /// <summary>
        /// 일반 검증 실패 결과 생성
        /// </summary>
        public static PathValidationResult CreateInvalid(string startNodeId, string targetNodeId, string error)
        {
            return new PathValidationResult
            {
                IsValidationRequired = true,
                IsValid = false,
                StartNodeId = startNodeId,
                TargetNodeId = targetNodeId,
                ValidationError = error
            };
        }
    }

    /// <summary>
    /// 되돌아가기 패턴 정보 (A → B → A)
    /// </summary>
    public class BacktrackingPattern
    {
        /// <summary>
        /// 시작 노드 (A)
        /// </summary>
        public string StartNode { get; set; }

        /// <summary>
        /// 중간 노드 (B)
        /// </summary>
        public string MiddleNode { get; set; }

        /// <summary>
        /// 되돌아간 노드 (다시 A)
        /// </summary>
        public string ReturnNode { get; set; }

        /// <summary>
        /// 경로에서의 시작 인덱스
        /// </summary>
        public int StartIndex { get; set; }

        /// <summary>
        /// 경로에서의 종료 인덱스
        /// </summary>
        public int EndIndex { get; set; }

        /// <summary>
        /// 기본 생성자
        /// </summary>
        public BacktrackingPattern()
        {
            StartNode = string.Empty;
            MiddleNode = string.Empty;
            ReturnNode = string.Empty;
            StartIndex = -1;
            EndIndex = -1;
        }

        /// <summary>
        /// 되돌아가기 패턴 생성
        /// </summary>
        public static BacktrackingPattern Create(string startNode, string middleNode, string returnNode, int startIndex, int endIndex)
        {
            return new BacktrackingPattern
            {
                StartNode = startNode,
                MiddleNode = middleNode,
                ReturnNode = returnNode,
                StartIndex = startIndex,
                EndIndex = endIndex
            };
        }

        /// <summary>
        /// 패턴 설명 문자열
        /// </summary>
        public override string ToString()
        {
            return $"{StartNode} → {MiddleNode} → {ReturnNode} (인덱스: {StartIndex}-{EndIndex})";
        }
    }
}