<x-app-layout>
    <x-slot name="header">
        <div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
            <div>
                <p class="text-sm font-semibold uppercase tracking-wide text-blue-600 dark:text-blue-400">Trade Details</p>
                <h2 class="mt-1 text-2xl font-bold text-slate-950 dark:text-white">{{ $trade->symbol }}</h2>
            </div>

            <div class="flex gap-2">
                <form method="POST" action="{{ route('trades.submit-review', $trade) }}">
                    @csrf
                    <button class="rounded-lg border border-blue-100 px-4 py-2 text-sm font-semibold text-blue-700 transition duration-200 hover:-translate-y-0.5 hover:bg-blue-50 dark:border-blue-900/50 dark:text-blue-300 dark:hover:bg-blue-950/60">
                        Submit for Review
                    </button>
                </form>
                <a href="{{ route('trades.edit', $trade) }}" class="rounded-lg border border-blue-100 px-4 py-2 text-sm font-semibold text-blue-700 transition duration-200 hover:-translate-y-0.5 hover:bg-blue-50 dark:border-blue-900/50 dark:text-blue-300 dark:hover:bg-blue-950/60">Edit</a>
                <a href="{{ route('trades.index') }}" class="rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white transition hover:bg-blue-700">All Trades</a>
            </div>
        </div>
    </x-slot>

    @if (session('success'))
        <div class="mb-6 rounded-lg border border-blue-100 dark:border-blue-900/50 bg-blue-50 dark:bg-blue-950/40 px-4 py-3 text-sm font-semibold text-blue-700 dark:text-blue-300">
            {{ session('success') }}
        </div>
    @endif

    <div class="grid gap-6 xl:grid-cols-3">
        <div class="rounded-lg border border-blue-100 dark:border-blue-900/50 bg-white dark:bg-slate-900 p-6 shadow-sm vt-card-motion xl:col-span-2">
            <dl class="grid gap-5 sm:grid-cols-2">
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Type</dt>
                    <dd class="mt-1 text-sm font-semibold capitalize text-slate-950 dark:text-white">{{ $trade->type }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Status</dt>
                    <dd class="mt-1 text-sm font-semibold capitalize text-slate-950 dark:text-white">{{ $trade->status }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Entry Price</dt>
                    <dd class="mt-1 text-sm font-semibold text-slate-950 dark:text-white">{{ $trade->entry_price }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Exit Price</dt>
                    <dd class="mt-1 text-sm font-semibold text-slate-950 dark:text-white">{{ $trade->exit_price ?? 'N/A' }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Quantity</dt>
                    <dd class="mt-1 text-sm font-semibold text-slate-950 dark:text-white">{{ $trade->quantity }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Profit/Loss</dt>
                    <dd class="mt-1 text-sm font-semibold {{ $trade->profit_loss >= 0 ? 'text-blue-600 dark:text-blue-400' : 'text-red-500 dark:text-red-400' }}">{{ $trade->profit_loss ?? 'N/A' }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Stop Loss</dt>
                    <dd class="mt-1 text-sm font-semibold text-slate-950 dark:text-white">{{ $trade->stop_loss ?? 'N/A' }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Take Profit</dt>
                    <dd class="mt-1 text-sm font-semibold text-slate-950 dark:text-white">{{ $trade->take_profit ?? 'N/A' }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Opened At</dt>
                    <dd class="mt-1 text-sm font-semibold text-slate-950 dark:text-white">{{ $trade->opened_at?->format('M d, Y H:i') ?? 'N/A' }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Closed At</dt>
                    <dd class="mt-1 text-sm font-semibold text-slate-950 dark:text-white">{{ $trade->closed_at?->format('M d, Y H:i') ?? 'N/A' }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Emotion</dt>
                    <dd class="mt-1 text-sm font-semibold text-slate-950 dark:text-white">{{ $trade->emotion ?? 'N/A' }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Trading Session</dt>
                    <dd class="mt-1 text-sm font-semibold text-slate-950 dark:text-white">{{ $trade->trading_session ?? 'N/A' }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Risk/Reward Followed</dt>
                    <dd class="mt-1 text-sm font-semibold text-slate-950 dark:text-white">{{ $trade->followed_risk_reward ? 'Yes' : 'No' }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Avoided Overtrading</dt>
                    <dd class="mt-1 text-sm font-semibold text-slate-950 dark:text-white">{{ $trade->avoided_overtrading ? 'Yes' : 'No' }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-slate-500 dark:text-slate-400">Review Status</dt>
                    <dd class="mt-1 text-sm font-semibold capitalize text-slate-950 dark:text-white">{{ str_replace('_', ' ', $trade->review_status ?? 'private') }}</dd>
                </div>
            </dl>

            <div class="mt-6 border-t border-blue-100 pt-6 dark:border-blue-900/50">
                <h3 class="text-sm font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Mistakes Tagged</h3>
                <div class="mt-3 flex flex-wrap gap-2">
                    @forelse ($trade->mistakeTags as $tag)
                        <span class="rounded-full bg-red-50 px-3 py-1 text-xs font-bold text-red-700 dark:bg-red-950/40 dark:text-red-200">{{ $tag->name }}</span>
                    @empty
                        <span class="text-sm text-slate-500 dark:text-slate-400">No mistakes tagged.</span>
                    @endforelse
                </div>
            </div>

            <div class="mt-6 border-t border-blue-100 pt-6 dark:border-blue-900/50">
                <h3 class="text-sm font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Trade Notes</h3>
                <p class="mt-2 whitespace-pre-line text-sm leading-6 text-slate-600 dark:text-slate-300">
                    {{ $trade->notes ?: 'No notes saved yet. Use the edit page to record your setup, emotion, mistake, or lesson.' }}
                </p>
            </div>

            <div class="mt-6 border-t border-blue-100 pt-6 dark:border-blue-900/50">
                <h3 class="text-sm font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Psychology Note</h3>
                <p class="mt-2 whitespace-pre-line text-sm leading-6 text-slate-600 dark:text-slate-300">
                    {{ $trade->psychology_note ?: 'No psychology note saved yet.' }}
                </p>
            </div>

            <form method="POST" action="{{ route('trades.destroy', $trade) }}" class="mt-6 border-t border-blue-100 dark:border-blue-900/50 pt-6" onsubmit="return confirm('Delete this trade?')">
                @csrf
                @method('DELETE')

                <button type="submit" class="rounded-lg bg-red-600 px-4 py-2 text-sm font-semibold text-white transition hover:bg-red-700">
                    Delete Trade
                </button>
            </form>
        </div>

        <div class="space-y-6">
            <div class="rounded-lg border border-blue-100 dark:border-blue-900/50 bg-white dark:bg-slate-900 p-6 shadow-sm vt-card-motion">
                <h3 class="text-lg font-bold text-slate-950 dark:text-white">Screenshot</h3>

                @if ($trade->screenshot_path)
                    <img src="{{ asset('storage/'.$trade->screenshot_path) }}" alt="Trade screenshot" class="mt-4 rounded-lg border border-blue-100 dark:border-blue-900/50">
                @else
                    <p class="mt-4 text-sm text-slate-500 dark:text-slate-400">No screenshot uploaded for this trade.</p>
                @endif
            </div>

            <div class="rounded-lg border border-blue-100 dark:border-blue-900/50 bg-white dark:bg-slate-900 p-6 shadow-sm vt-card-motion">
                <h3 class="text-lg font-bold text-slate-950 dark:text-white">Mentor Comments</h3>
                <div class="mt-4 space-y-3">
                    @forelse ($trade->comments ?? [] as $comment)
                        <div class="rounded-lg bg-blue-50 p-4 dark:bg-blue-950/30">
                            <p class="text-sm text-slate-700 dark:text-slate-200">{{ $comment->comment }}</p>
                            <p class="mt-2 text-xs font-bold text-slate-500 dark:text-slate-400">{{ $comment->mentor->name }} - {{ $comment->created_at->format('M d, Y') }}</p>
                        </div>
                    @empty
                        <p class="text-sm text-slate-500 dark:text-slate-400">No mentor comments yet.</p>
                    @endforelse
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
