@extends('layouts.app')

@section('title', 'Machine Health Monitoring')

@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">Machine Health</li>
        </ol>
    </nav>

    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-0 fw-bold">Machine Health Monitoring</h4>
            <p class="text-muted small mb-0">Predictive Maintenance & Asset Reliability Analytics</p>
        </div>
        <form action="{{ route('intelligence.recalculate') }}" method="POST">
            @csrf
            <button type="submit" class="btn btn-primary">
                <i class="bi bi-arrow-clockwise me-2"></i> Refresh Analytics
            </button>
        </form>
    </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">Machine Asset</th>
                            <th>Line / Zone</th>
                            <th>Health Score</th>
                            <th>Failure Prob.</th>
                            <th>Estimated Window</th>
                            <th class="text-end pe-4">Recommendation</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($machines as $hs)
                            <tr>
                                <td class="ps-4">
                                    <div class="fw-bold">{{ $hs->machine->name }}</div>
                                    <small class="text-muted text-uppercase smallest">{{ $hs->machine->model ?? 'Standard Model' }}</small>
                                </td>
                                <td>
                                    <span class="badge bg-light text-dark fw-normal border">
                                        {{ $hs->machine->productionLine->name }}
                                    </span>
                                </td>
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <div class="progress flex-grow-1" style="height: 6px; width: 80px;">
                                            <div class="progress-bar {{ $hs->health_score > 80 ? 'bg-success' : ($hs->health_score > 60 ? 'bg-warning' : 'bg-danger') }}" 
                                                role="progressbar" style="width: {{ $hs->health_score }}%"></div>
                                        </div>
                                        <span class="fw-bold">{{ $hs->health_score }}%</span>
                                    </div>
                                </td>
                                <td>
                                    <span class="text-{{ $hs->failure_probability > 40 ? 'danger' : 'muted' }} fw-bold">
                                        {{ $hs->failure_probability }}%
                                    </span>
                                </td>
                                <td>
                                    @if($hs->estimated_failure_window_start)
                                        <div class="small fw-bold">{{ $hs->estimated_failure_window_start->format('M d') }} - {{ $hs->estimated_failure_window_end->format('M d') }}</div>
                                        <div class="smallest text-muted">Analysis based on runtime</div>
                                    @else
                                        <span class="text-muted small">No immediate risk</span>
                                    @endif
                                </td>
                                <td class="text-end pe-4">
                                    <div class="smaller {{ $hs->health_score < 70 ? 'text-danger fw-bold' : 'text-muted' }}">
                                        {{ $hs->recommendation }}
                                    </div>
                                    @if($hs->health_score < 70)
                                        <a href="{{ route('maintenance.create', ['machine_id' => $hs->machine_id]) }}" class="btn btn-sm btn-outline-danger py-0 px-2 mt-1 smallest">Schedule Now</a>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center py-5 text-muted">Initialize data to see machine health predictions.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($machines->hasPages())
            <div class="card-footer bg-white py-3">
                {{ $machines->links() }}
            </div>
        @endif
    </div>

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