{{-- File: resources/views/seats/statistics.blade.php --}}

@extends('layout.main')

@section('title', 'সীট পরিসংখ্যান')

@section('content')
@php
    // ======================
    // Helpers: Bangla Digits
    // ======================
    $bn = function ($value) {
        return strtr((string)$value, [
            '0'=>'০','1'=>'১','2'=>'২','3'=>'৩','4'=>'৪',
            '5'=>'৫','6'=>'৬','7'=>'৭','8'=>'৮','9'=>'৯',
        ]);
    };

    $bnFormat = function ($number, $decimals = 0) use ($bn) {
        $formatted = number_format((float)$number, $decimals);
        return $bn($formatted);
    };

    $seatTypeLabel = function ($type) {
        return match ($type) {
            'parliament' => 'জাতীয় সংসদ',
            'local' => 'স্থানীয়',
            'city_corp' => 'সিটি কর্পোরেশন',
            'pouroshova' => 'পৌরসভা',
            'union' => 'ইউনিয়ন',
            default => $type ?: 'অনির্ধারিত',
        };
    };
@endphp

<div class="min-h-screen bg-gray-50 p-4 sm:p-6">
    <div class="max-w-7xl mx-auto">
        {{-- Header --}}
        <div class="mb-8">
            <div class="flex items-center justify-between mb-6">
                <div>
                    <h1 class="text-2xl sm:text-3xl font-bold text-gray-900 mb-2">সীট পরিসংখ্যান</h1>
                    <p class="text-gray-600">সকল সীটের সংক্ষিপ্ত পরিসংখ্যান</p>
                </div>
                <a href="{{ route('seats.index') }}"
                   class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition-colors">
                    সীট তালিকা
                </a>
            </div>
        </div>

        {{-- Summary Cards --}}
        <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 mb-8">
            <div class="bg-white p-6 rounded-xl shadow-sm border border-gray-200">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-500">মোট সীট</p>
                        <p class="text-3xl font-bold text-gray-900">{{ $bn($totalSeats ?? 0) }}</p>
                    </div>
                    <div class="w-12 h-12 bg-green-100 rounded-lg flex items-center justify-center">
                        <i class="fas fa-chair text-green-600 text-xl"></i>
                    </div>
                </div>
                <p class="text-xs text-gray-500 mt-2">
                    <span class="text-green-600">{{ $bn($activeSeats ?? 0) }}</span> সক্রিয় সীট
                </p>
            </div>

            <div class="bg-white p-6 rounded-xl shadow-sm border border-gray-200">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-500">মোট ভোটার</p>
                        <p class="text-3xl font-bold text-green-700">{{ $bnFormat($totalVoters ?? 0, 0) }}</p>
                    </div>
                    <div class="w-12 h-12 bg-blue-100 rounded-lg flex items-center justify-center">
                        <i class="fas fa-users text-blue-600 text-xl"></i>
                    </div>
                </div>
                <p class="text-xs text-gray-500 mt-2">
                    গড় {{ $bnFormat(($totalSeats ?? 0) > 0 ? (($totalVoters ?? 0) / $totalSeats) : 0, 0) }} ভোটার/সীট
                </p>
            </div>

            <div class="bg-white p-6 rounded-xl shadow-sm border border-gray-200">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-500">মোট প্রার্থী</p>
                        <p class="text-3xl font-bold text-blue-700">{{ $bn($totalCandidates ?? 0) }}</p>
                    </div>
                    <div class="w-12 h-12 bg-purple-100 rounded-lg flex items-center justify-center">
                        <i class="fas fa-user-tie text-purple-600 text-xl"></i>
                    </div>
                </div>
                <p class="text-xs text-gray-500 mt-2">
                    গড় {{ $bnFormat(($totalSeats ?? 0) > 0 ? (($totalCandidates ?? 0) / $totalSeats) : 0, 1) }} প্রার্থী/সীট
                </p>
            </div>

            <div class="bg-white p-6 rounded-xl shadow-sm border border-gray-200">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-500">গড় ভোটার/প্রার্থী</p>
                        <p class="text-3xl font-bold text-purple-700">
                            {{ ($totalCandidates ?? 0) > 0 ? $bnFormat(($totalVoters ?? 0) / $totalCandidates, 0) : '০' }}
                        </p>
                    </div>
                    <div class="w-12 h-12 bg-yellow-100 rounded-lg flex items-center justify-center">
                        <i class="fas fa-chart-line text-yellow-600 text-xl"></i>
                    </div>
                </div>
                <p class="text-xs text-gray-500 mt-2">
                    প্রতি প্রার্থীর জন্য গড় ভোটার
                </p>
            </div>
        </div>

        {{-- Division-wise Statistics --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden mb-8">
            <div class="px-6 py-4 border-b border-gray-200 flex items-center justify-between gap-3 flex-wrap">
                <h2 class="text-lg font-semibold text-gray-900">বিভাগ অনুযায়ী সীট বণ্টন</h2>
                <p class="text-sm text-gray-500">
                    মোট বিভাগ: <span class="font-semibold">{{ $bn(($divisions ?? collect())->count()) }}</span>
                </p>
            </div>

            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                                বিভাগ
                            </th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                                সীট সংখ্যা
                            </th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                                ভোটার সংখ্যা
                            </th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                                গড় ভোটার/সীট
                            </th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                                শতকরা
                            </th>
                        </tr>
                    </thead>

                    <tbody class="bg-white divide-y divide-gray-200">
                        @foreach(($divisions ?? collect()) as $division)
                            @php
                                // expects: $division->name_bn, $division->seat_count, $division->voters
                                $seatCount = (int)($division->seat_count ?? 0);
                                $votersSum = (int)($division->voters ?? 0);

                                $percent = ($totalSeats ?? 0) > 0 ? ($seatCount / $totalSeats) * 100 : 0;
                                $avg = $seatCount > 0 ? ($votersSum / $seatCount) : 0;
                            @endphp
                            <tr class="hover:bg-gray-50 transition-colors">
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm font-medium text-gray-900">
                                        {{ $division->name_bn ?? 'অনির্ধারিত' }}
                                    </div>
                                </td>

                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm text-gray-900">{{ $bn($seatCount) }}</div>
                                </td>

                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm text-gray-900">{{ $bnFormat($votersSum, 0) }}</div>
                                </td>

                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm text-gray-900">{{ $bnFormat($avg, 0) }}</div>
                                </td>

                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="flex items-center">
                                        <div class="w-full bg-gray-200 rounded-full h-2">
                                            <div class="bg-green-600 h-2 rounded-full"
                                                 style="width: {{ $percent }}%"></div>
                                        </div>
                                        <span class="ml-2 text-sm text-gray-900">
                                            {{ $bnFormat($percent, 1) }}%
                                        </span>
                                    </div>
                                </td>
                            </tr>
                        @endforeach

                        @if(($divisions ?? collect())->isEmpty())
                            <tr>
                                <td colspan="5" class="px-6 py-8 text-center text-sm text-gray-600">
                                    কোনো তথ্য পাওয়া যায়নি
                                </td>
                            </tr>
                        @endif
                    </tbody>
                </table>
            </div>
        </div>

        {{-- Seat Type Statistics + Active --}}
        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
                <h2 class="text-lg font-semibold text-gray-900 mb-4">সীটের ধরন অনুযায়ী সীট</h2>

                <div class="space-y-4">
                    @foreach(($seatTypes ?? collect()) as $type)
                        @php
                            // expects: $type->seat_type, $type->count
                            $count = (int)($type->count ?? 0);
                            $typePercent = ($totalSeats ?? 0) > 0 ? ($count / $totalSeats) * 100 : 0;
                            $label = $seatTypeLabel($type->seat_type ?? null);
                        @endphp

                        <div>
                            <div class="flex justify-between text-sm mb-1">
                                <span class="text-gray-700">{{ $label }}</span>
                                <span class="font-medium text-gray-900">
                                    {{ $bn($count) }} ({{ $bnFormat($typePercent, 1) }}%)
                                </span>
                            </div>
                            <div class="w-full bg-gray-200 rounded-full h-2">
                                <div class="bg-blue-600 h-2 rounded-full"
                                     style="width: {{ $typePercent }}%"></div>
                            </div>
                        </div>
                    @endforeach

                    @if(($seatTypes ?? collect())->isEmpty())
                        <p class="text-sm text-gray-600">কোনো তথ্য পাওয়া যায়নি</p>
                    @endif
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
                <h2 class="text-lg font-semibold text-gray-900 mb-4">সক্রিয়তা অবস্থা</h2>

                <div class="flex items-center justify-center">
                    <div class="relative w-48 h-48">
                        <svg class="w-full h-full" viewBox="0 0 100 100">
                            @php
                                $activePercentage = ($totalSeats ?? 0) > 0 ? (($activeSeats ?? 0) / $totalSeats) * 100 : 0;
                                $dash = $activePercentage * 2.513; // 251.3 circle length
                            @endphp

                            <circle cx="50" cy="50" r="40" fill="transparent"
                                    stroke="#e5e7eb" stroke-width="10" />

                            <circle cx="50" cy="50" r="40" fill="transparent"
                                    stroke="#10b981" stroke-width="10"
                                    stroke-dasharray="{{ $dash }} 251.3"
                                    stroke-dashoffset="0"
                                    transform="rotate(-90 50 50)" />

                            <text x="50" y="45" text-anchor="middle" dy=".3em"
                                  class="text-xl font-bold fill-gray-900">
                                {{ $bnFormat($activePercentage, 1) }}%
                            </text>
                            <text x="50" y="65" text-anchor="middle" dy=".3em"
                                  class="text-sm fill-gray-500 ">
                                সক্রিয়
                            </text>
                        </svg>
                    </div>
                </div>

                <div class="mt-4 text-center space-y-2">
                    <div class="flex justify-center items-center gap-4">
                        <div class="flex items-center gap-2">
                            <div class="w-3 h-3 bg-green-500 rounded-full"></div>
                            <span class="text-sm text-gray-700">সক্রিয়: {{ $bn($activeSeats ?? 0) }}</span>
                        </div>
                        <div class="flex items-center gap-2">
                            <div class="w-3 h-3 bg-gray-300 rounded-full"></div>
                            <span class="text-sm text-gray-700">
                                নিষ্ক্রিয়: {{ $bn(max(0, ($totalSeats ?? 0) - ($activeSeats ?? 0))) }}
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
@endsection
