@extends('layouts.app')

@section('title', 'Raw Material Forecast')

@section('content')
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ route('intelligence.dashboard') }}">Intelligence</a></li>
            <li class="breadcrumb-item active">Material Forecast</li>
        </ol>
    </nav>

    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-0 fw-bold">Raw Material Forecast</h4>
            <p class="text-muted small mb-0">Predicted Stock Depletion & Purchase Recommendations</p>
        </div>
        <form action="{{ route('intelligence.recalculate') }}" method="POST">
            @csrf
            <button type="submit" class="btn btn-primary">
                <i class="bi bi-graph-up-arrow me-2"></i> Update Forecast
            </button>
        </form>
    </div>

    <div class="card border-0 shadow-sm text-white mb-4" style="background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);">
        <div class="card-body p-4">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <h5 class="fw-bold mb-2">Predictive Supply Chain Management</h5>
                    <p class="mb-0 opacity-75 small">This analysis combines current inventory movements with active production plans to predict the exact day your stock will run out. Purchase recommendations are calculated based on a 14-day safety buffer.</p>
                </div>
                <div class="col-md-4 text-md-end mt-3 mt-md-0">
                    <div class="smallest text-uppercase opacity-50 mb-1">Forecast Confidence</div>
                    <div class="h4 mb-0 fw-bold">88.5%</div>
                </div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">Raw Material</th>
                            <th>Current Stock</th>
                            <th>Avg. Daily Usage</th>
                            <th>Days Remaining</th>
                            <th>Predicted Shortage</th>
                            <th class="text-end pe-4">Recommended Purchase</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($forecasts as $f)
                            @php
                                $daysRemaining = $f->predicted_shortage_at ? now()->diffInDays($f->predicted_shortage_at, false) : 0;
                            @endphp
                            <tr>
                                <td class="ps-4">
                                    <div class="fw-bold">{{ $f->product->name }}</div>
                                    <small class="text-muted text-uppercase smallest">SKU: {{ $f->product->sku }}</small>
                                </td>
                                <td>
                                    <div class="fw-bold">{{ number_format($f->current_stock, 2) }} {{ $f->product->unit_of_measure }}</div>
                                </td>
                                <td>{{ number_format($f->daily_consumption_avg, 2) }} / day</td>
                                <td>
                                    <span class="badge {{ $daysRemaining < 5 ? 'bg-danger' : ($daysRemaining < 10 ? 'bg-warning' : 'bg-success') }} rounded-pill">
                                        {{ max(0, round($daysRemaining, 1)) }} Days
                                    </span>
                                </td>
                                <td>
                                    @if($f->predicted_shortage_at)
                                        <div class="small fw-bold {{ $daysRemaining < 5 ? 'text-danger' : '' }}">
                                            {{ $f->predicted_shortage_at->format('M d, Y') }}
                                        </div>
                                    @else
                                        <span class="text-muted small">Stable stock</span>
                                    @endif
                                </td>
                                <td class="text-end pe-4">
                                    <div class="fw-bold text-primary">{{ number_format($f->recommended_purchase_quantity, 2) }} {{ $f->product->unit_of_measure }}</div>
                                    <a href="{{ route('purchase-orders.create', ['product_id' => $f->product_id]) }}" class="btn btn-sm btn-link p-0 text-decoration-none smallest fw-bold">Create Order</a>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center py-5 text-muted">No material forecast data available.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($forecasts->hasPages())
            <div class="card-footer bg-white py-3">
                {{ $forecasts->links() }}
            </div>
        @endif
    </div>

    <style>
        .smallest { font-size: 0.7rem; }
    </style>
@endsection
