import SwiftUI

enum Route: Hashable {
    // AI
    case aiChat(context: ChatEntryContext? = nil)
    case dailyThinking
    case aiFeedback
    case activeRecall
    case weakPoints
    case reviewCard

    // Library
    case librarySearch
    case libraryDetail(knowledgeBaseId: String)
    case libraryImport
    case libraryCreate
    case addKnowledge(knowledgeBaseId: String)
    case knowledgeDetail(item: KnowledgeItem)
    case editKnowledge(item: KnowledgeItem)

    // Study
    case learningSession(taskTitle: String, taskType: String, taskColorHex: String)
    case studyHome

    // Import
    case importReview(sourceId: String)

    // Quiz
    case quizList(knowledgeBaseId: String)
    case quizTake(quizId: String)
    case quizResult(quizId: String, attemptId: String)

    // Material Reader
    case materialReader(materialId: String, filePath: String, materialType: MaterialType, knowledgeBaseId: String? = nil, title: String = "")
    case materialDetail(knowledgeBaseId: String, fileName: String, fileType: MaterialType, fileSize: UInt64, filePath: String, uploadDate: String?)

    // Profile
    case notificationList
    case settings
    case goalSetting
    case methodPreference
    case feedbackForm
    case editProfile
}

extension Route {
    var label: String {
        switch self {
        case .aiChat: "AIChatPage"
        case .dailyThinking: "DailyThinkingPage"
        case .aiFeedback: "AIFeedbackPageView"
        case .activeRecall: "ActiveRecallView"
        case .weakPoints: "WeakPointsPage"
        case .reviewCard: "ReviewCardView"
        case .librarySearch: "LibrarySearchView"
        case .libraryDetail: "LibraryDetailPage"
        case .libraryImport: "ImportPage"
        case .libraryCreate: "CreateLibraryPage"
        case .addKnowledge: "AddKnowledgePage"
        case .knowledgeDetail: "KnowledgeDetailPage"
        case .editKnowledge: "EditKnowledgePage"
        case .learningSession: "LearningSessionView"
        case .studyHome: "StudyHomeView"
        case .notificationList: "NotificationListView"
        case .settings: "SettingsView"
        case .goalSetting: "GoalSettingDetailView"
        case .methodPreference: "MethodPreferenceView"
        case .feedbackForm: "FeedbackFormView"
        case .editProfile: "EditProfilePage"
        case .importReview: "ImportReviewPage"
        case .quizList: "QuizListView"
        case .quizTake: "QuizTakerView"
        case .quizResult: "QuizResultView"
        case .materialReader: "MaterialReaderView"
        case .materialDetail: "MaterialDetailView"
        }
    }

    @ViewBuilder
    var destination: some View {
        let content: AnyView = {
            switch self {
            case .aiChat(let ctx):   AnyView(AIChatPage(context: ctx))
            case .dailyThinking:      AnyView(DailyThinkingPage())
            case .aiFeedback:         AnyView(AIFeedbackPageView())
            case .activeRecall:       AnyView(ActiveRecallView())
            case .weakPoints:         AnyView(WeakPointsPage())
            case .reviewCard:         AnyView(ReviewCardView())

            case .librarySearch:      AnyView(LibrarySearchView())
            case .libraryDetail(let id): AnyView(LibraryDetailPage(knowledgeBaseId: id))
            case .libraryImport:      AnyView(ImportPage())
            case .libraryCreate:      AnyView(CreateLibraryPage())
            case .addKnowledge(let id): AnyView(AddKnowledgePage(knowledgeBaseId: id))
            case .knowledgeDetail(let item): AnyView(KnowledgeDetailPage(item: item))
            case .editKnowledge(let item): AnyView(EditKnowledgePage(item: item))

            case .learningSession(let title, let type, let colorHex):
                AnyView(LearningSessionView(taskTitle: title, taskType: type, taskColor: Color(hex: colorHex)))
            case .studyHome:          AnyView(StudyHomeView(selectedTab: .constant("study")))

            case .notificationList:   AnyView(NotificationListView())
            case .settings:           AnyView(SettingsView())
            case .goalSetting:        AnyView(GoalSettingDetailView())
            case .methodPreference:   AnyView(MethodPreferenceView())
            case .feedbackForm:       AnyView(FeedbackFormView())
            case .editProfile:        AnyView(EditProfilePage())
            case .importReview(let sourceId): AnyView(ImportReviewPage(sourceId: sourceId))
            case .quizList(let kbId):        AnyView(QuizListView(knowledgeBaseId: kbId))
            case .quizTake(let id):          AnyView(QuizTakerView(quizId: id))
            case .quizResult(let qid, let aid): AnyView(QuizResultView(quizId: qid, attemptId: aid))
            case .materialReader(let mid, let path, let mt, let kbId, let title): AnyView(MaterialReaderView(materialId: mid, filePath: path, materialType: mt, knowledgeBaseId: kbId, title: title))
            case .materialDetail(let kbId, let name, let type, let size, let path, let date):
                AnyView(MaterialDetailView(knowledgeBaseId: kbId, fileName: name, fileType: type, fileSize: size, filePath: path, uploadDate: date))
            }
        }()

        #if DEBUG
        content.overlay(alignment: .topTrailing) {
            Text(label)
                .font(.system(size: 13, weight: .semibold, design: .monospaced))
                .foregroundColor(.white)
                .padding(.horizontal, 8).padding(.vertical, 4)
                .background(Color.black.opacity(0.65))
                .cornerRadius(6)
                .padding(.top, 60).padding(.trailing, 4)
                .onTapGesture { UIPasteboard.general.string = label }
        }
        #else
        content
        #endif
    }
}
