<Window x:Class="AxCopilot.Views.WorkflowAnalyzerWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="AX Agent — 워크플로우 분석기" Width="560" Height="720"
        MinWidth="420" MinHeight="400"
        WindowStartupLocation="Manual"
        WindowStyle="None" AllowsTransparency="True" Background="Transparent"
        ResizeMode="CanResizeWithGrip">
    <Border Background="{DynamicResource LauncherBackground}"
            BorderBrush="{DynamicResource BorderColor}" BorderThickness="1"
            CornerRadius="12" Margin="6"
            >
        <Border.Effect>
            <DropShadowEffect BlurRadius="18" ShadowDepth="4" Opacity="0.3" Color="Black" Direction="270"/>
        </Border.Effect>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="44"/>   <!-- 타이틀 바 -->
                <RowDefinition Height="Auto"/> <!-- 요약 카드 -->
                <RowDefinition Height="Auto"/> <!-- 탭 바 -->
                <RowDefinition Height="*"/>    <!-- 콘텐츠 (타임라인 또는 병목 분석) -->
                <RowDefinition Height="Auto"/> <!-- 상세 패널 -->
                <RowDefinition Height="32"/>   <!-- 상태 바 -->
            </Grid.RowDefinitions>

            <!-- ═══ 타이틀 바 ═══ -->
            <Grid Grid.Row="0" Background="Transparent" MouseLeftButtonDown="TitleBar_MouseLeftButtonDown">
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="16,0,0,0">
                    <TextBlock Text="&#xE9D9;" FontFamily="Segoe MDL2 Assets" FontSize="14"
                               Foreground="{DynamicResource AccentColor}" VerticalAlignment="Center" Margin="0,0,8,0"/>
                    <TextBlock Text="워크플로우 분석기" FontSize="14" FontWeight="SemiBold"
                               Foreground="{DynamicResource PrimaryText}" VerticalAlignment="Center"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,0,8,0">
                    <Border x:Name="BtnClear" Width="32" Height="32" CornerRadius="6" Cursor="Hand"
                            Background="Transparent" ToolTip="초기화"
                            MouseLeftButtonUp="BtnClear_Click"
                            MouseEnter="TitleBtn_MouseEnter" MouseLeave="TitleBtn_MouseLeave">
                        <TextBlock Text="&#xE74D;" FontFamily="Segoe MDL2 Assets" FontSize="12"
                                   Foreground="{DynamicResource SecondaryText}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                    <Border x:Name="BtnMinimize" Width="32" Height="32" CornerRadius="6" Cursor="Hand"
                            Background="Transparent" ToolTip="최소화"
                            MouseLeftButtonUp="BtnMinimize_Click"
                            MouseEnter="TitleBtn_MouseEnter" MouseLeave="TitleBtn_MouseLeave">
                        <TextBlock Text="&#xE921;" FontFamily="Segoe MDL2 Assets" FontSize="10"
                                   Foreground="{DynamicResource SecondaryText}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                    <Border x:Name="BtnClose" Width="32" Height="32" CornerRadius="6" Cursor="Hand"
                            Background="Transparent" ToolTip="닫기"
                            MouseLeftButtonUp="BtnClose_Click"
                            MouseEnter="TitleBtn_MouseEnter" MouseLeave="TitleBtn_MouseLeave">
                        <TextBlock Text="&#xE8BB;" FontFamily="Segoe MDL2 Assets" FontSize="10"
                                   Foreground="{DynamicResource SecondaryText}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </StackPanel>
            </Grid>

            <!-- ═══ 요약 카드 ═══ -->
            <Border Grid.Row="1" Margin="12,0,12,6">
                <UniformGrid Columns="4" Margin="0">
                    <Border Background="{DynamicResource ItemBackground}" CornerRadius="8" Padding="10,8" Margin="2">
                        <StackPanel>
                            <TextBlock Text="소요 시간" FontSize="10" Foreground="{DynamicResource SecondaryText}"/>
                            <TextBlock x:Name="CardElapsed" Text="0.0s" FontSize="16" FontWeight="Bold" Foreground="#60A5FA"/>
                        </StackPanel>
                    </Border>
                    <Border Background="{DynamicResource ItemBackground}" CornerRadius="8" Padding="10,8" Margin="2">
                        <StackPanel>
                            <TextBlock Text="반복" FontSize="10" Foreground="{DynamicResource SecondaryText}"/>
                            <TextBlock x:Name="CardIterations" Text="0" FontSize="16" FontWeight="Bold" Foreground="#A78BFA"/>
                        </StackPanel>
                    </Border>
                    <Border Background="{DynamicResource ItemBackground}" CornerRadius="8" Padding="10,8" Margin="2"
                            ToolTip="입력 토큰 / 출력 토큰">
                        <StackPanel>
                            <TextBlock Text="토큰" FontSize="10" Foreground="{DynamicResource SecondaryText}"/>
                            <TextBlock x:Name="CardTokens" Text="0" FontSize="16" FontWeight="Bold" Foreground="#34D399"/>
                            <StackPanel Orientation="Horizontal" Margin="0,2,0,0">
                                <TextBlock Text="↑" FontSize="9" Foreground="#3B82F6" VerticalAlignment="Center"/>
                                <TextBlock x:Name="CardInputTokens" Text="0" FontSize="9" Foreground="#3B82F6" Margin="1,0,6,0" VerticalAlignment="Center"/>
                                <TextBlock Text="↓" FontSize="9" Foreground="#10B981" VerticalAlignment="Center"/>
                                <TextBlock x:Name="CardOutputTokens" Text="0" FontSize="9" Foreground="#10B981" Margin="1,0,0,0" VerticalAlignment="Center"/>
                            </StackPanel>
                        </StackPanel>
                    </Border>
                    <Border Background="{DynamicResource ItemBackground}" CornerRadius="8" Padding="10,8" Margin="2">
                        <StackPanel>
                            <TextBlock Text="도구 호출" FontSize="10" Foreground="{DynamicResource SecondaryText}"/>
                            <TextBlock x:Name="CardToolCalls" Text="0" FontSize="16" FontWeight="Bold" Foreground="#FBBF24"/>
                        </StackPanel>
                    </Border>
                </UniformGrid>
            </Border>

            <!-- ═══ 탭 바 ═══ -->
            <Border Grid.Row="2" Margin="12,0,12,6">
                <StackPanel Orientation="Horizontal">
                    <Border x:Name="TabTimeline" CornerRadius="6" Padding="12,6" Margin="0,0,4,0"
                            Cursor="Hand" MouseLeftButtonUp="TabTimeline_Click">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock x:Name="TabTimelineIcon" Text="&#xE916;" FontFamily="Segoe MDL2 Assets" FontSize="11"
                                       Foreground="{DynamicResource PrimaryText}" VerticalAlignment="Center" Margin="0,0,6,0"/>
                            <TextBlock x:Name="TabTimelineText" Text="타임라인" FontSize="12" FontWeight="SemiBold"
                                       Foreground="{DynamicResource PrimaryText}" VerticalAlignment="Center"/>
                        </StackPanel>
                    </Border>
                    <Border x:Name="TabBottleneck" CornerRadius="6" Padding="12,6" Margin="0,0,4,0"
                            Cursor="Hand" MouseLeftButtonUp="TabBottleneck_Click">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock x:Name="TabBottleneckIcon" Text="&#xE9D2;" FontFamily="Segoe MDL2 Assets" FontSize="11"
                                       Foreground="{DynamicResource PrimaryText}" VerticalAlignment="Center" Margin="0,0,6,0"/>
                            <TextBlock x:Name="TabBottleneckText" Text="병목 분석" FontSize="12" FontWeight="SemiBold"
                                       Foreground="{DynamicResource PrimaryText}" VerticalAlignment="Center"/>
                        </StackPanel>
                    </Border>
                    <!-- Phase 18-B: 리플레이 탭 -->
                    <Border x:Name="TabReplay" CornerRadius="6" Padding="12,6" Margin="0,0,4,0"
                            Cursor="Hand" MouseLeftButtonUp="TabReplay_Click">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock x:Name="TabReplayIcon" Text="&#xE768;" FontFamily="Segoe MDL2 Assets" FontSize="11"
                                       Foreground="{DynamicResource PrimaryText}" VerticalAlignment="Center" Margin="0,0,6,0"/>
                            <TextBlock x:Name="TabReplayText" Text="리플레이" FontSize="12" FontWeight="SemiBold"
                                       Foreground="{DynamicResource PrimaryText}" VerticalAlignment="Center"/>
                        </StackPanel>
                    </Border>
                </StackPanel>
            </Border>

            <!-- ═══ 콘텐츠: 타임라인 ═══ -->
            <ScrollViewer Grid.Row="3" x:Name="TimelineScroller" VerticalScrollBarVisibility="Auto"
                          Margin="12,0,12,0" Padding="0,0,4,0">
                <StackPanel x:Name="TimelinePanel" Margin="0,0,0,8"/>
            </ScrollViewer>

            <!-- ═══ 콘텐츠: 병목 분석 ═══ -->
            <ScrollViewer Grid.Row="3" x:Name="BottleneckScroller" VerticalScrollBarVisibility="Auto"
                          Margin="12,0,12,0" Padding="0,0,4,0" Visibility="Collapsed">
                <StackPanel x:Name="BottleneckPanel" Margin="0,0,0,8">
                    <!-- 워터폴 차트 -->
                    <TextBlock Text="⏱ 실행 흐름 (워터폴)" FontSize="13" FontWeight="SemiBold"
                               Foreground="{DynamicResource PrimaryText}" Margin="4,4,0,8"/>
                    <Border Background="{DynamicResource ItemBackground}" CornerRadius="8"
                            Padding="12,8" Margin="0,0,0,12" MinHeight="60">
                        <Canvas x:Name="WaterfallCanvas" Height="120" ClipToBounds="True"/>
                    </Border>

                    <!-- 도구별 소요시간 -->
                    <TextBlock Text="🔧 도구별 소요시간" FontSize="13" FontWeight="SemiBold"
                               Foreground="{DynamicResource PrimaryText}" Margin="4,4,0,8"/>
                    <Border Background="{DynamicResource ItemBackground}" CornerRadius="8"
                            Padding="12,8" Margin="0,0,0,12">
                        <StackPanel x:Name="ToolTimePanel"/>
                    </Border>

                    <!-- 토큰 추세 -->
                    <TextBlock Text="📈 반복별 토큰 추세" FontSize="13" FontWeight="SemiBold"
                               Foreground="{DynamicResource PrimaryText}" Margin="4,4,0,8"/>
                    <Border Background="{DynamicResource ItemBackground}" CornerRadius="8"
                            Padding="12,8" Margin="0,0,0,8" MinHeight="60">
                        <Canvas x:Name="TokenTrendCanvas" Height="100" ClipToBounds="True"/>
                    </Border>
                </StackPanel>
            </ScrollViewer>

            <!-- ═══ 콘텐츠: 리플레이 (Phase 18-B) ═══ -->
            <ScrollViewer Grid.Row="3" x:Name="ReplayScroller" VerticalScrollBarVisibility="Auto"
                          Margin="12,0,12,0" Padding="0,0,4,0" Visibility="Collapsed">
                <StackPanel x:Name="ReplayPanel" Margin="0,4,0,8">
                    <!-- 세션 선택 -->
                    <TextBlock Text="📂 저장된 세션" FontSize="13" FontWeight="SemiBold"
                               Foreground="{DynamicResource PrimaryText}" Margin="4,0,0,6"/>
                    <Border Background="{DynamicResource ItemBackground}" CornerRadius="8"
                            Padding="8,6" Margin="0,0,0,8" MaxHeight="120">
                        <ScrollViewer VerticalScrollBarVisibility="Auto">
                            <StackPanel x:Name="ReplaySessionList"/>
                        </ScrollViewer>
                    </Border>
                    <!-- 재생 컨트롤 -->
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,8">
                        <Border x:Name="BtnReplayStart" CornerRadius="6" Padding="12,6" Cursor="Hand"
                                Background="{DynamicResource AccentColor}" Margin="0,0,6,0"
                                MouseLeftButtonUp="BtnReplayStart_Click">
                            <TextBlock Text="▶ 재생" FontSize="12" Foreground="White" FontWeight="SemiBold"/>
                        </Border>
                        <Border x:Name="BtnReplayStop" CornerRadius="6" Padding="12,6" Cursor="Hand"
                                Background="{DynamicResource ItemBackground}" Margin="0,0,6,0"
                                MouseLeftButtonUp="BtnReplayStop_Click">
                            <TextBlock Text="■ 중단" FontSize="12" Foreground="{DynamicResource PrimaryText}" FontWeight="SemiBold"/>
                        </Border>
                        <TextBlock Text="속도 (ms):" FontSize="11" Foreground="{DynamicResource SecondaryText}"
                                   VerticalAlignment="Center" Margin="8,0,6,0"/>
                        <Slider x:Name="ReplaySpeedSlider" Minimum="50" Maximum="1000" Value="200"
                                Width="80" VerticalAlignment="Center" ValueChanged="ReplaySpeedSlider_Changed"/>
                        <TextBlock x:Name="ReplaySpeedLabel" Text="200ms" FontSize="11"
                                   Foreground="{DynamicResource SecondaryText}" VerticalAlignment="Center" Margin="4,0,0,0"/>
                    </StackPanel>
                    <!-- 진행 표시줄 -->
                    <ProgressBar x:Name="ReplayProgress" Height="4"
                                 Foreground="{DynamicResource AccentColor}"
                                 Background="{DynamicResource ItemBackground}"
                                 Margin="0,0,0,8" Minimum="0" Maximum="100" Value="0"/>
                    <!-- 재생 이벤트 스트림 -->
                    <TextBlock Text="▶ 이벤트 스트림" FontSize="13" FontWeight="SemiBold"
                               Foreground="{DynamicResource PrimaryText}" Margin="4,0,0,6"/>
                    <Border Background="{DynamicResource ItemBackground}" CornerRadius="8"
                            Padding="8,6" Margin="0,0,0,4" MinHeight="60">
                        <ScrollViewer x:Name="ReplayEventScroll" VerticalScrollBarVisibility="Auto" MaxHeight="200">
                            <StackPanel x:Name="ReplayEventPanel"/>
                        </ScrollViewer>
                    </Border>
                </StackPanel>
            </ScrollViewer>

            <!-- ═══ 상세 패널 ═══ -->
            <Border Grid.Row="4" x:Name="DetailPanel" Visibility="Collapsed"
                    Background="{DynamicResource ItemBackground}" CornerRadius="8"
                    Margin="12,4,12,4" Padding="14,10" MaxHeight="180">
                <ScrollViewer VerticalScrollBarVisibility="Auto">
                    <StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,4">
                            <TextBlock x:Name="DetailTitle" Text="" FontSize="13" FontWeight="SemiBold"
                                       Foreground="{DynamicResource PrimaryText}"/>
                            <TextBlock x:Name="DetailBadge" Text="" FontSize="10" Foreground="White"
                                       Margin="8,0,0,0" VerticalAlignment="Center" Padding="6,1"
                                       Background="#34D399"/>
                        </StackPanel>
                        <TextBlock x:Name="DetailMeta" Text="" FontSize="11"
                                   Foreground="{DynamicResource SecondaryText}" Margin="0,0,0,4"/>
                        <TextBlock x:Name="DetailContent" Text="" FontSize="11.5" TextWrapping="Wrap"
                                   Foreground="{DynamicResource PrimaryText}" MaxHeight="100"
                                   LineHeight="17"/>
                    </StackPanel>
                </ScrollViewer>
            </Border>

            <!-- ═══ 상태 바 ═══ -->
            <Border Grid.Row="5" Margin="12,0,12,6" ClipToBounds="True">
                <Grid>
                    <TextBlock x:Name="StatusText" Text="대기 중..." FontSize="11"
                               Foreground="{DynamicResource SecondaryText}" VerticalAlignment="Center"
                               TextTrimming="CharacterEllipsis" MaxWidth="400"/>
                    <TextBlock x:Name="LogLevelBadge" Text="" FontSize="10" HorizontalAlignment="Right"
                               Foreground="{DynamicResource SecondaryText}" VerticalAlignment="Center"
                               Padding="6,1" Opacity="0.7"/>
                </Grid>
            </Border>
        </Grid>
    </Border>
</Window>
