@extends('layouts.default')
@section('title', 'Rapor Detayı')
@section('content')
<div class="container-fluid">
    @include('admin.partials.page-header', ['actions' => new \Illuminate\Support\HtmlString('<a href="'.route('admin.reports.archive').'" class="btn btn-light me-2">Arşive Dön</a><a href="'.route('admin.reports.create').'" class="btn btn-primary">Yeni Rapor</a>')])

    <div class="row">
        <div class="col-xl-8">
            <div class="card">
                <div class="card-header border-0 pb-0">
                    <h4 class="card-title mb-0">{{ $report->client?->company_name }}</h4>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-3 mb-3"><span class="text-muted d-block">Rapor Tipi</span><strong>{{ ['daily' => 'Günlük', 'weekly' => 'Haftalık', 'monthly' => 'Aylık', 'custom' => 'Özel'][$report->report_type] ?? $report->report_type }}</strong></div>
                        <div class="col-md-3 mb-3"><span class="text-muted d-block">Başlangıç</span><strong>{{ $report->date_from?->format('d.m.Y') }}</strong></div>
                        <div class="col-md-3 mb-3"><span class="text-muted d-block">Bitiş</span><strong>{{ $report->date_to?->format('d.m.Y') }}</strong></div>
                        <div class="col-md-3 mb-3"><span class="text-muted d-block">Durum</span>@include('admin.partials.status-badge', ['status' => $report->status])</div>
                    </div>

                    @if($report->sent_at)
                        <div class="alert alert-success py-2">Bu rapor son olarak {{ $report->sent_at->format('d.m.Y H:i') }} tarihinde gönderildi.</div>
                    @endif

                    @if($report->error_message)
                        <div class="alert alert-danger">{{ $report->error_message }}</div>
                    @endif

                    @php($overview = $report->summary['overview'] ?? [])
                    <div class="row mt-3">
                        @foreach([
                            'total_ad_spend' => 'Toplam Reklam Harcaması',
                            'total_ad_clicks' => 'Reklam Tıklaması',
                            'total_ad_impressions' => 'Reklam Gösterimi',
                            'total_conversions' => 'Toplam Dönüşüm',
                            'organic_clicks' => 'Organik Tıklama',
                            'ga4_sessions' => 'GA4 Oturum',
                        ] as $key => $label)
                            <div class="col-md-4 mb-3">
                                <div class="border rounded p-3 h-100">
                                    <span class="text-muted d-block">{{ $label }}</span>
                                    <strong class="fs-4">{{ is_numeric($overview[$key] ?? null) ? number_format((float) $overview[$key], 2, ',', '.') : ($overview[$key] ?? '-') }}</strong>
                                </div>
                            </div>
                        @endforeach
                    </div>

                    <h5 class="mt-4">Otomatik Notlar</h5>
                    <ul class="mb-0">
                        @foreach(($report->summary['notes'] ?? []) as $note)
                            <li>{{ $note }}</li>
                        @endforeach
                    </ul>
                </div>
            </div>

            @include('admin.reports.partials.ai-insights', ['report' => $report])

            <div class="card">
                <div class="card-header border-0 pb-0"><h4 class="card-title mb-0">Mail Gönderim Geçmişi</h4></div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-responsive-md align-middle">
                            <thead><tr><th>Tarih</th><th>Alıcı</th><th>Konu</th><th>Durum</th><th>Hata</th></tr></thead>
                            <tbody>
                            @forelse($report->emailLogs as $log)
                                <tr><td>{{ $log->sent_at?->format('d.m.Y H:i') ?? $log->created_at?->format('d.m.Y H:i') }}</td><td>{{ implode(', ', $log->recipients ?? []) }}</td><td>{{ $log->subject }}</td><td>@include('admin.partials.status-badge', ['status' => $log->status])</td><td class="text-danger small">{{ \Illuminate\Support\Str::limit($log->error_message, 80) }}</td></tr>
                            @empty
                                <tr><td colspan="5" class="text-center text-muted py-4">Bu rapor henüz mail ile gönderilmedi.</td></tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-4">
            <div class="card">
                <div class="card-header border-0 pb-0">
                    <h4 class="card-title mb-0">AI Yorum İşlemleri</h4>
                </div>
                <div class="card-body">
                    <form action="{{ route('admin.reports.ai.generate', $report) }}" method="POST">
                        @csrf
                        <div class="form-check mb-2"><input class="form-check-input" type="checkbox" name="regenerate_files" value="1" id="regenerate-files" checked><label class="form-check-label" for="regenerate-files">Yorumdan sonra rapor dosyalarını yenile</label></div>
                        <div class="form-check mb-3"><input class="form-check-input" type="checkbox" name="force_local" value="1" id="force-local"><label class="form-check-label" for="force-local">Harici API yerine yerel kural motorunu kullan</label></div>
                        <button class="btn btn-primary w-100 mb-2" @disabled($report->status !== 'completed')>AI Yorumu Üret / Yenile</button>
                    </form>

                    @if($report->hasAiInsights())
                        <form action="{{ route('admin.reports.ai.clear', $report) }}" method="POST">
                            @csrf
                            @method('DELETE')
                            <button class="btn btn-outline-danger w-100">AI Yorumunu Temizle</button>
                        </form>
                    @endif

                    <a href="{{ route('admin.ai.settings') }}" class="btn btn-light w-100 mt-2">AI Ayarları</a>
                </div>
            </div>

            <div class="card">
                <div class="card-header border-0 pb-0">
                    <h4 class="card-title mb-0">Dosyalar</h4>
                </div>
                <div class="card-body">
                    @if($report->html_path)
                        <a href="{{ route('admin.reports.download', [$report, 'html']) }}" class="btn btn-outline-primary w-100 mb-2">HTML / Yazdırılabilir Rapor</a>
                    @endif
                    @if($report->pdf_path)
                        <a href="{{ route('admin.reports.download', [$report, 'pdf']) }}" class="btn btn-outline-danger w-100 mb-2">PDF İndir</a>
                    @else
                        <div class="alert alert-warning py-2">PDF motoru kurulu değilse sistem HTML rapor üretir. DomPDF kurulduğunda PDF otomatik oluşur.</div>
                    @endif
                    @if($report->excel_path)
                        <a href="{{ route('admin.reports.download', [$report, 'excel']) }}" class="btn btn-outline-success w-100 mb-2">Excel Uyumlu XLS İndir</a>
                    @endif
                    @if($report->csv_path)
                        <a href="{{ route('admin.reports.download', [$report, 'csv']) }}" class="btn btn-outline-secondary w-100 mb-2">CSV İndir</a>
                    @endif
                    <form action="{{ route('admin.reports.regenerate', $report) }}" method="POST" class="mt-3">
                        @csrf
                        <button class="btn btn-primary w-100">Yeniden Oluştur</button>
                    </form>
                </div>
            </div>

            <div class="card">
                <div class="card-header border-0 pb-0"><h4 class="card-title mb-0">Mail Gönder</h4></div>
                <div class="card-body">
                    <form method="POST" action="{{ route('admin.reports.send', $report) }}">
                        @csrf
                        <div class="mb-3"><label class="form-label">Alıcılar</label><textarea name="recipients_raw" class="form-control" rows="3" placeholder="Boş bırakılırsa müşteri alıcıları kullanılır">{{ old('recipients_raw', implode("\n", $report->client?->report_recipients ?? [])) }}</textarea></div>
                        <div class="mb-3"><label class="form-label">CC</label><textarea name="cc_raw" class="form-control" rows="2">{{ old('cc_raw') }}</textarea></div>
                        <div class="mb-3"><label class="form-label">Konu</label><input name="subject" class="form-control" value="{{ old('subject') }}" placeholder="Boşsa otomatik oluşturulur"></div>
                        <div class="mb-3"><label class="form-label">Kısa Not</label><textarea name="note" class="form-control" rows="3">{{ old('note') }}</textarea></div>
                        <div class="form-check mb-2"><input class="form-check-input" type="checkbox" name="include_pdf" value="1" id="mail-pdf" checked><label class="form-check-label" for="mail-pdf">PDF/HTML raporu ekle</label></div>
                        <div class="form-check mb-2"><input class="form-check-input" type="checkbox" name="include_excel" value="1" id="mail-excel" checked><label class="form-check-label" for="mail-excel">Excel ekle</label></div>
                        <div class="form-check mb-3"><input class="form-check-input" type="checkbox" name="include_csv" value="1" id="mail-csv"><label class="form-check-label" for="mail-csv">CSV ekle</label></div>
                        <button class="btn btn-success w-100" @disabled($report->status !== 'completed')>Raporu Mail Gönder</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
