@extends('layouts.dashboard')

@section('title', 'Promotion Summary Report')

@section('content')
<div class="content-wrapper">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-1">
                    <li class="breadcrumb-item"><a href="{{ route('dashboard.reports') }}">Reports</a></li>
                    <li class="breadcrumb-item active">Promotion Summary</li>
                </ol>
            </nav>
            <h4 class="mb-0">Promotion Summary Report</h4>
        </div>
        @if($canExport)
        <a href="{{ route('dashboard.reports.promotion-summary.export', request()->query()) }}" class="btn btn-success">
            <i class="bi bi-file-earmark-excel me-1"></i> Export to Excel
        </a>
        @endif
    </div>

    <!-- Filters -->
    <div class="card border-0 shadow-sm mb-4">
        <div class="card-body">
            <form method="GET" action="{{ route('dashboard.reports.promotion-summary') }}" class="row g-3">
                @if(isset($clients) && $clients->count() > 0)
                <div class="col-md-3">
                    <label class="form-label">Organization</label>
                    <select name="client_id" class="form-select">
                        <option value="">All Organizations</option>
                        @foreach($clients as $client)
                        <option value="{{ $client->id }}" {{ ($clientId ?? '') == $client->id ? 'selected' : '' }}>
                            {{ $client->name }}
                        </option>
                        @endforeach
                    </select>
                </div>
                @endif
                <div class="col-md-2">
                    <label class="form-label">Date From</label>
                    <input type="date" name="date_from" class="form-control" value="{{ $dateFrom }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">Date To</label>
                    <input type="date" name="date_to" class="form-control" value="{{ $dateTo }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">Status</label>
                    <select name="status" class="form-select">
                        <option value="">All Status</option>
                        <option value="sent" {{ $status == 'sent' ? 'selected' : '' }}>Sent</option>
                        <option value="delivered" {{ $status == 'delivered' ? 'selected' : '' }}>Delivered</option>
                        <option value="failed" {{ $status == 'failed' ? 'selected' : '' }}>Failed</option>
                        <option value="pending" {{ $status == 'pending' ? 'selected' : '' }}>Pending</option>
                    </select>
                </div>
                <div class="col-md-4">
                    <label class="form-label">User</label>
                    <select name="user_id" class="form-select">
                        <option value="">All Users</option>
                        @foreach($users as $user)
                        <option value="{{ $user->id }}" {{ $userId == $user->id ? 'selected' : '' }}>
                            {{ $user->name }}
                        </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="bi bi-funnel me-1"></i> Filter
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Summary Stats -->
    <div class="row g-3 mb-4">
        <div class="col-md-2">
            <div class="card border-0 shadow-sm">
                <div class="card-body text-center">
                    <h3 class="text-primary mb-0">{{ number_format($summary['total']) }}</h3>
                    <small class="text-muted">Total Promotions</small>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-0 shadow-sm bg-success bg-opacity-10">
                <div class="card-body text-center">
                    <h3 class="text-success mb-0">{{ number_format($summary['sent']) }}</h3>
                    <small class="text-muted">Sent</small>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-0 shadow-sm bg-info bg-opacity-10">
                <div class="card-body text-center">
                    <h3 class="text-info mb-0">{{ number_format($summary['delivered']) }}</h3>
                    <small class="text-muted">Delivered</small>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-0 shadow-sm bg-danger bg-opacity-10">
                <div class="card-body text-center">
                    <h3 class="text-danger mb-0">{{ number_format($summary['failed']) }}</h3>
                    <small class="text-muted">Failed</small>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-0 shadow-sm bg-warning bg-opacity-10">
                <div class="card-body text-center">
                    <h3 class="text-warning mb-0">{{ number_format($summary['pending']) }}</h3>
                    <small class="text-muted">Pending</small>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-0 shadow-sm bg-secondary bg-opacity-10">
                <div class="card-body text-center">
                    <h3 class="text-secondary mb-0">
                        {{ $summary['total'] > 0 ? round(($summary['delivered'] / $summary['total']) * 100, 1) : 0 }}%
                    </h3>
                    <small class="text-muted">Delivery Rate</small>
                </div>
            </div>
        </div>
    </div>

    <!-- Chart -->
    <div class="row g-4 mb-4">
        <div class="col-md-8">
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-transparent">
                    <h6 class="mb-0">Promotion Trend</h6>
                </div>
                <div class="card-body">
                    <canvas id="promoTrendChart" height="120"></canvas>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-transparent">
                    <h6 class="mb-0">Status Distribution</h6>
                </div>
                <div class="card-body">
                    <canvas id="promoStatusChart" height="200"></canvas>
                </div>
            </div>
        </div>
    </div>

    <!-- Report Table -->
    <div class="card border-0 shadow-sm">
        <div class="card-header bg-transparent">
            <h6 class="mb-0">Promotion Details</h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>User</th>
                            <th>Phone Number</th>
                            <th>Status</th>
                            <th>Trigger Type</th>
                            <th>Message</th>
                            <th>Sent At</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($report as $log)
                        <tr>
                            <td>{{ $log->id }}</td>
                            <td>{{ $log->user->name ?? 'N/A' }}</td>
                            <td><span class="masked-data">{{ $log->phone_number }}</span></td>
                            <td>
                                @switch($log->status)
                                @case('sent')
                                <span class="badge badge-sent">Sent</span>
                                @break
                                @case('delivered')
                                <span class="badge bg-info">Delivered</span>
                                @break
                                @case('failed')
                                <span class="badge badge-failed">Failed</span>
                                @break
                                @case('pending')
                                <span class="badge badge-pending">Pending</span>
                                @break
                                @default
                                <span class="badge bg-secondary">{{ $log->status }}</span>
                                @endswitch
                            </td>
                            <td>
                                @switch($log->trigger_type)
                                @case('incoming')
                                <span class="badge badge-incoming">Incoming</span>
                                @break
                                @case('outgoing')
                                <span class="badge badge-outgoing">Outgoing</span>
                                @break
                                @case('missed')
                                <span class="badge badge-missed">Missed</span>
                                @break
                                @default
                                <span class="badge bg-secondary">{{ $log->trigger_type ?? 'N/A' }}</span>
                                @endswitch
                            </td>
                            <td>
                                <span class="text-truncate d-inline-block" style="max-width: 250px;" title="{{ $log->message }}">
                                    {{ $log->message }}
                                </span>
                            </td>
                            <td>{{ $log->sent_at ? \Carbon\Carbon::parse($log->sent_at)->format('Y-m-d H:i:s') : 'Pending' }}</td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">No promotions found for the selected criteria.</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <div class="d-flex justify-content-center mt-4">
                {{ $report->appends(request()->query())->links() }}
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Promotion Trend Chart
        const promoByDay = @json($promoByDay);
        const labels = Object.keys(promoByDay);
        const data = Object.values(promoByDay);

        new Chart(document.getElementById('promoTrendChart'), {
            type: 'bar',
            data: {
                labels: labels,
                datasets: [{
                    label: 'Promotions',
                    data: data,
                    backgroundColor: 'rgba(253, 126, 20, 0.8)',
                    borderRadius: 4
                }]
            },
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        display: false
                    }
                },
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });

        // Status Distribution Chart
        const promoByStatus = @json($promoByStatus);
        new Chart(document.getElementById('promoStatusChart'), {
            type: 'doughnut',
            data: {
                labels: Object.keys(promoByStatus).map(s => s.charAt(0).toUpperCase() + s.slice(1)),
                datasets: [{
                    data: Object.values(promoByStatus),
                    backgroundColor: ['#28a745', '#17a2b8', '#dc3545', '#ffc107']
                }]
            },
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        position: 'bottom'
                    }
                }
            }
        });
    });
</script>
@endpush
@endsection