@extends('admin.layouts.app')
@section('title', 'Feasibility Report #'.str_pad($report->id, 5, '0', STR_PAD_LEFT))
@section('breadcrumb')
    <a href="{{ route('admin.feasibility.index') }}" class="breadcrumb-link">Reports</a>
    <span class="breadcrumb-sep">/</span>
    <span class="current">#{{ str_pad($report->id, 5, '0', STR_PAD_LEFT) }}</span>
@endsection
@section('content')
@php
    $score = $report->feasibility_score ?? 0;
    $scoreColor = $score >= 70 ? 'var(--vsc-green-terminal)' : ($score >= 40 ? 'var(--vsc-orange)' : 'var(--vsc-red)');
    $scoreLabel = $score >= 70 ? 'Feasible' : ($score >= 40 ? 'Marginal' : 'Challenging');
@endphp

<div class="page-header" style="display:flex;align-items:center;justify-content:space-between;">
    <div>
        <h1 class="page-title">
            <span class="title-icon"><i data-lucide="file-bar-chart" style="width:20px;height:20px;"></i></span>
            Report #{{ str_pad($report->id, 5, '0', STR_PAD_LEFT) }}
        </h1>
        <p class="page-subtitle">{{ $report->address }}</p>
    </div>
    <div style="display:flex;gap:8px;">
        @if($report->status === 'completed')
        <a href="{{ route('admin.feasibility.pdf', $report->id) }}" class="btn btn-secondary" target="_blank">
            <i data-lucide="download" style="width:13px;height:13px;"></i> PDF
        </a>
        @endif
        <a href="{{ route('admin.feasibility.index') }}" class="btn btn-secondary">
            <i data-lucide="arrow-left" style="width:13px;height:13px;"></i> Back
        </a>
    </div>
</div>

<div style="display:grid;grid-template-columns:2fr 1fr;gap:20px;">

    {{-- Left Column --}}
    <div style="display:flex;flex-direction:column;gap:20px;">

        {{-- Location & Zone --}}
        <div class="admin-table-wrap">
            <div class="admin-table-header"><div class="admin-table-title"><i data-lucide="map-pin" class="icon" style="width:13px;height:13px;"></i> Location &amp; Zone</div></div>
            <div style="padding:16px;display:grid;grid-template-columns:1fr 1fr;gap:12px;">
                <div><div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:4px;">Address</div><div style="font-size:13px;color:var(--vsc-text-active);">{{ $report->address }}</div></div>
                <div><div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:4px;">Coordinates</div><div style="font-size:12px;font-family:monospace;color:var(--vsc-teal);">{{ $report->latitude }}, {{ $report->longitude }}</div></div>
                <div><div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:4px;">State</div><div style="font-size:13px;">{{ $report->state?->name ?? '—' }}</div></div>
                <div><div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:4px;">City</div><div style="font-size:13px;">{{ $report->city?->name ?? '—' }}</div></div>
                <div><div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:4px;">Zone</div><div><span class="badge badge-blue" style="font-size:12px;">{{ $report->zone?->name ?? '—' }}</span></div></div>
                <div><div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:4px;">Road Width</div><div style="font-size:13px;color:var(--vsc-blue);">{{ $report->road_width_m ? $report->road_width_m.' m' : '—' }}</div></div>
                @if($report->google_maps_url)
                <div style="grid-column:1/-1;"><div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:4px;">Google Maps URL</div><div style="font-size:12px;"><a href="{{ $report->google_maps_url }}" target="_blank" rel="noopener" style="color:var(--vsc-blue);word-break:break-all;">{{ Str::limit($report->google_maps_url, 80) }}</a></div></div>
                @endif
            </div>
        </div>

        {{-- Calculation Results --}}
        <div class="admin-table-wrap">
            <div class="admin-table-header"><div class="admin-table-title"><i data-lucide="calculator" class="icon" style="width:13px;height:13px;"></i> Calculation Results</div></div>
            <div style="padding:16px;">
                <div style="display:grid;grid-template-columns:repeat(3,1fr);gap:12px;margin-bottom:16px;">
                    <div style="background:var(--vsc-bg-input);border:1px solid var(--vsc-border-light);border-radius:6px;padding:12px;text-align:center;">
                        <div style="font-size:22px;font-weight:700;color:var(--vsc-yellow);">{{ $report->plot_area_sqm ? number_format($report->plot_area_sqm) : '—' }}</div>
                        <div style="font-size:10px;color:var(--vsc-text-secondary);margin-top:2px;">Plot Area (sqm)</div>
                    </div>
                    <div style="background:var(--vsc-bg-input);border:1px solid var(--vsc-border-light);border-radius:6px;padding:12px;text-align:center;">
                        <div style="font-size:22px;font-weight:700;color:var(--vsc-blue);">{{ $report->max_buildable_area_sqm ? number_format($report->max_buildable_area_sqm) : '—' }}</div>
                        <div style="font-size:10px;color:var(--vsc-text-secondary);margin-top:2px;">Max Buildable (sqm)</div>
                    </div>
                    <div style="background:var(--vsc-bg-input);border:1px solid var(--vsc-border-light);border-radius:6px;padding:12px;text-align:center;">
                        <div style="font-size:22px;font-weight:700;color:var(--vsc-teal);">{{ $report->ground_coverage_sqm ? number_format($report->ground_coverage_sqm) : '—' }}</div>
                        <div style="font-size:10px;color:var(--vsc-text-secondary);margin-top:2px;">Ground Coverage (sqm)</div>
                    </div>
                    <div style="background:var(--vsc-bg-input);border:1px solid var(--vsc-border-light);border-radius:6px;padding:12px;text-align:center;">
                        <div style="font-size:22px;font-weight:700;color:var(--vsc-orange);">{{ $report->max_floors ?? '—' }}</div>
                        <div style="font-size:10px;color:var(--vsc-text-secondary);margin-top:2px;">Max Floors</div>
                    </div>
                    @if($report->zoneRule)
                    <div style="background:var(--vsc-bg-input);border:1px solid var(--vsc-border-light);border-radius:6px;padding:12px;text-align:center;">
                        <div style="font-size:22px;font-weight:700;color:var(--vsc-purple);">{{ $report->zoneRule->max_far }}</div>
                        <div style="font-size:10px;color:var(--vsc-text-secondary);margin-top:2px;">FAR / FSI</div>
                    </div>
                    <div style="background:var(--vsc-bg-input);border:1px solid var(--vsc-border-light);border-radius:6px;padding:12px;text-align:center;">
                        <div style="font-size:22px;font-weight:700;color:var(--vsc-green-terminal);">{{ $report->zoneRule->max_height_m ? $report->zoneRule->max_height_m.'m' : '—' }}</div>
                        <div style="font-size:10px;color:var(--vsc-text-secondary);margin-top:2px;">Max Height</div>
                    </div>
                    @endif
                </div>

                @if($report->setback_summary)
                <div style="background:var(--vsc-bg-input);border:1px solid var(--vsc-border-light);border-radius:6px;padding:12px;margin-bottom:12px;">
                    <div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:8px;letter-spacing:.06em;">Setbacks</div>
                    <div style="display:flex;gap:16px;flex-wrap:wrap;">
                        @foreach($report->setback_summary as $key => $val)
                            <span style="font-size:12px;color:var(--vsc-text-secondary);">{{ ucfirst($key) }}: <b style="color:var(--vsc-text-active);">{{ $val }}m</b></span>
                        @endforeach
                    </div>
                </div>
                @endif

                @if($report->permitted_uses)
                <div style="background:var(--vsc-bg-input);border:1px solid var(--vsc-border-light);border-radius:6px;padding:12px;">
                    <div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:8px;letter-spacing:.06em;">Permitted Uses</div>
                    <div style="display:flex;gap:6px;flex-wrap:wrap;">
                        @foreach($report->permitted_uses as $use)
                            <span class="badge badge-teal" style="font-size:11px;">{{ $use }}</span>
                        @endforeach
                    </div>
                </div>
                @endif
            </div>
        </div>

        {{-- Restriction Flags --}}
        @if($report->restriction_flags && count($report->restriction_flags))
        <div class="admin-table-wrap">
            <div class="admin-table-header"><div class="admin-table-title"><i data-lucide="alert-triangle" class="icon" style="width:13px;height:13px;color:var(--vsc-orange);"></i> Restriction Flags</div></div>
            <div style="padding:16px;display:flex;flex-direction:column;gap:8px;">
                @foreach($report->restriction_flags as $flag => $active)
                    <div style="display:flex;align-items:center;gap:8px;font-size:13px;color:{{ $active ? 'var(--vsc-orange)' : 'var(--vsc-green, #3fb950)' }};">
                        <i data-lucide="{{ $active ? 'alert-circle' : 'check-circle' }}" style="width:14px;height:14px;flex-shrink:0;"></i>
                        {{ str_replace('_', ' ', ucfirst($flag)) }}
                        @if($active)
                            <span style="font-size:11px;background:rgba(255,140,0,0.12);padding:1px 8px;border-radius:4px;">Yes</span>
                        @else
                            <span style="font-size:11px;background:rgba(63,185,80,0.12);padding:1px 8px;border-radius:4px;">No</span>
                        @endif
                    </div>
                @endforeach
            </div>
        </div>
        @endif

        {{-- Cost & ROI Estimates --}}
        @php $ce = $report->cost_estimates ?? []; @endphp
        @if(!empty($ce))
        <div class="admin-table-wrap">
            <div class="admin-table-header"><div class="admin-table-title"><i data-lucide="indian-rupee" class="icon" style="width:13px;height:13px;"></i> Cost &amp; ROI Estimates</div></div>
            <div style="padding:16px;">
                <div style="display:grid;grid-template-columns:repeat(3,1fr);gap:12px;margin-bottom:12px;">
                    <div style="background:var(--vsc-bg-input);border:1px solid var(--vsc-border-light);border-radius:6px;padding:12px;text-align:center;">
                        <div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:4px;">Construction Cost</div>
                        <div style="font-size:14px;font-weight:700;color:var(--vsc-text-active);">₹{{ number_format($ce['construction_cost_min_inr'] ?? 0) }} – ₹{{ number_format($ce['construction_cost_max_inr'] ?? 0) }}</div>
                        <div style="font-size:10px;color:var(--vsc-text-secondary);margin-top:2px;">indicative range</div>
                    </div>
                    <div style="background:var(--vsc-bg-input);border:1px solid var(--vsc-border-light);border-radius:6px;padding:12px;text-align:center;">
                        <div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:4px;">Gross Dev. Value</div>
                        <div style="font-size:14px;font-weight:700;color:var(--vsc-blue);">₹{{ number_format($ce['gross_development_value_inr'] ?? 0) }}</div>
                        <div style="font-size:10px;color:var(--vsc-text-secondary);margin-top:2px;">market value at completion</div>
                    </div>
                    <div style="background:var(--vsc-bg-input);border:1px solid var(--vsc-border-light);border-radius:6px;padding:12px;text-align:center;">
                        <div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:4px;">Est. Profit</div>
                        <div style="font-size:14px;font-weight:700;color:{{ ($ce['estimated_profit_inr'] ?? 0) > 0 ? 'var(--vsc-green-terminal)' : 'var(--vsc-red)' }};">₹{{ number_format($ce['estimated_profit_inr'] ?? 0) }}</div>
                        <div style="font-size:10px;color:var(--vsc-text-secondary);margin-top:2px;">GDV minus max construction cost</div>
                    </div>
                    <div style="background:var(--vsc-bg-input);border:1px solid var(--vsc-border-light);border-radius:6px;padding:12px;text-align:center;">
                        <div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:4px;">ROI</div>
                        <div style="font-size:22px;font-weight:700;color:{{ ($ce['roi_pct'] ?? 0) >= 15 ? 'var(--vsc-green-terminal)' : (($ce['roi_pct'] ?? 0) >= 5 ? 'var(--vsc-orange)' : 'var(--vsc-red)') }};">{{ $ce['roi_pct'] ?? '—' }}%</div>
                        <div style="font-size:10px;color:var(--vsc-text-secondary);margin-top:2px;">return on investment</div>
                    </div>
                    <div style="background:var(--vsc-bg-input);border:1px solid var(--vsc-border-light);border-radius:6px;padding:12px;text-align:center;">
                        <div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:4px;">Monthly Rent Est.</div>
                        <div style="font-size:14px;font-weight:700;color:var(--vsc-teal);">₹{{ number_format($ce['monthly_rent_estimate_inr'] ?? 0) }}</div>
                        <div style="font-size:10px;color:var(--vsc-text-secondary);margin-top:2px;">indicative rental income</div>
                    </div>
                    <div style="background:var(--vsc-bg-input);border:1px solid var(--vsc-border-light);border-radius:6px;padding:12px;text-align:center;">
                        <div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:4px;">Break-even</div>
                        <div style="font-size:22px;font-weight:700;color:var(--vsc-text-active);">{{ $ce['breakeven_years'] ?? '—' }}</div>
                        <div style="font-size:10px;color:var(--vsc-text-secondary);margin-top:2px;">years (from rental)</div>
                    </div>
                </div>
                <div style="font-size:10px;color:var(--vsc-text-secondary);font-style:italic;">
                    ⚠ Cost &amp; ROI figures are indicative estimates based on state-level benchmarks. Actual values may vary.
                    Project type: {{ ucfirst($report->project_type ?? 'residential') }}.
                    @if(!empty($ce['note'])) {{ $ce['note'] }} @endif
                </div>
            </div>
        </div>
        @endif

        {{-- Calculation Log --}}
        @if($report->calculation_log)
        <div class="admin-table-wrap">
            <div class="admin-table-header"><div class="admin-table-title"><i data-lucide="list" class="icon" style="width:13px;height:13px;"></i> Calculation Log</div></div>
            <div style="padding:12px 16px;">
                @foreach($report->calculation_log as $entry)
                    <div style="font-size:11px;color:var(--vsc-text-secondary);font-family:monospace;padding:2px 0;border-bottom:1px solid var(--vsc-border-light);">
                        @if(is_array($entry))
                            <strong>{{ $entry['step'] ?? '' }}:</strong> {{ $entry['result'] ?? '' }}
                        @else
                            {{ $entry }}
                        @endif
                    </div>
                @endforeach
            </div>
        </div>
        @endif
    </div>

    {{-- Right Column --}}
    <div style="display:flex;flex-direction:column;gap:20px;">
        {{-- Score Card --}}
        <div class="admin-table-wrap" style="text-align:center;">
            <div class="admin-table-header"><div class="admin-table-title"><i data-lucide="award" class="icon" style="width:13px;height:13px;"></i> Feasibility Score</div></div>
            <div style="padding:32px 16px;">
                <div style="font-size:64px;font-weight:800;color:{{ $scoreColor }};line-height:1;">{{ $report->feasibility_score ?? '—' }}</div>
                <div style="font-size:14px;font-weight:600;color:{{ $scoreColor }};margin-top:4px;">{{ $scoreLabel }}</div>
                <div style="width:100%;height:8px;background:var(--vsc-bg-input);border-radius:4px;margin-top:16px;overflow:hidden;">
                    <div style="width:{{ $score }}%;height:100%;background:{{ $scoreColor }};border-radius:4px;transition:width .4s;"></div>
                </div>
                <div style="font-size:10px;color:var(--vsc-text-secondary);margin-top:4px;">out of 100</div>
            </div>
        </div>

        {{-- Meta --}}
        <div class="admin-table-wrap">
            <div class="admin-table-header"><div class="admin-table-title"><i data-lucide="info" class="icon" style="width:13px;height:13px;"></i> Report Meta</div></div>
            <div style="padding:14px;display:flex;flex-direction:column;gap:10px;">
                <div>
                    <div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:3px;">Status</div>
                    @php $statusBadge = ['pending'=>'badge-orange','processing'=>'badge-blue','completed'=>'badge-green','failed'=>'badge-red'][$report->status ?? 'pending'] @endphp
                    <span class="badge {{ $statusBadge }}">{{ ucfirst($report->status ?? 'pending') }}</span>
                </div>
                <div>
                    <div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:3px;">User</div>
                    <div style="font-size:13px;">{{ $report->user?->name ?? 'Guest' }}</div>
                </div>
                <div>
                    <div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:3px;">Zone Rule Applied</div>
                    <div style="font-size:12px;color:var(--vsc-blue);">{{ $report->zoneRule?->rule_source ?? '—' }}</div>
                </div>
                <div>
                    <div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:3px;">Created</div>
                    <div style="font-size:12px;color:var(--vsc-text-secondary);">{{ $report->created_at?->format('d M Y, H:i') ?? '—' }}</div>
                </div>
                @if($report->error_message)
                <div>
                    <div style="font-size:10px;text-transform:uppercase;color:var(--vsc-red);margin-bottom:3px;">Error</div>
                    <div style="font-size:11px;color:var(--vsc-red);">{{ $report->error_message }}</div>
                </div>
                @endif
            </div>
        </div>

        {{-- Zone Rule Details --}}
        @if($report->zoneRule)
        <div class="admin-table-wrap">
            <div class="admin-table-header"><div class="admin-table-title"><i data-lucide="sliders" class="icon" style="width:13px;height:13px;"></i> Applied Zone Rule</div></div>
            <div style="padding:14px;display:flex;flex-direction:column;gap:8px;font-size:12px;">
                <div style="display:flex;justify-content:space-between;"><span style="color:var(--vsc-text-secondary);">FAR</span><b style="color:var(--vsc-yellow);">{{ $report->zoneRule->max_far }}</b></div>
                <div style="display:flex;justify-content:space-between;"><span style="color:var(--vsc-text-secondary);">Max Height</span><b>{{ $report->zoneRule->max_height_m ? $report->zoneRule->max_height_m.'m' : '—' }}</b></div>
                <div style="display:flex;justify-content:space-between;"><span style="color:var(--vsc-text-secondary);">Ground Coverage</span><b>{{ $report->zoneRule->max_ground_coverage ? ($report->zoneRule->max_ground_coverage * 100).'%' : '—' }}</b></div>
                <div style="display:flex;justify-content:space-between;"><span style="color:var(--vsc-text-secondary);">Road Width</span><b>{{ $report->zoneRule->road_width_label }}</b></div>
                <div style="display:flex;justify-content:space-between;"><span style="color:var(--vsc-text-secondary);">Source</span><span style="color:var(--vsc-blue);font-size:11px;">{{ $report->zoneRule->rule_source ?? '—' }}</span></div>
                <div style="display:flex;justify-content:space-between;"><span style="color:var(--vsc-text-secondary);">Parking Ratio</span><b>{{ $report->zoneRule->parking_ratio_ecs ? $report->zoneRule->parking_ratio_ecs.' ECS/sqm' : '—' }}</b></div>
                @if($report->zoneRule->special_conditions)
                <div style="margin-top:4px;padding-top:6px;border-top:1px solid var(--vsc-border-light);">
                    <div style="font-size:10px;text-transform:uppercase;color:var(--vsc-text-secondary);margin-bottom:3px;">Special Conditions</div>
                    <div style="font-size:11px;color:var(--vsc-orange);font-style:italic;">{{ $report->zoneRule->special_conditions }}</div>
                </div>
                @endif
            </div>
        </div>
        @endif
    </div>
</div>
@endsection
