@extends('layouts.default')
@section('title', 'Uyarı Kuralları')
@section('content')
<div class="container-fluid">
    @include('admin.partials.page-header', ['actions' => new \Illuminate\Support\HtmlString('
        <div class="d-flex gap-2">
            <a href="'.route('admin.alert-logs.index').'" class="btn btn-outline-primary">Uyarı Geçmişi</a>
            <form method="POST" action="'.route('admin.alert-rules.evaluate-all').'" class="d-inline">'.csrf_field().'<button class="btn btn-outline-warning">Tüm Kuralları Çalıştır</button></form>
            <a href="'.route('admin.alert-rules.create').'" class="btn btn-primary">Kural Ekle</a>
        </div>
    ')])

    <div class="row">
        <div class="col-xl-3 col-sm-6">
            <div class="card"><div class="card-body"><h6 class="text-muted">Açık Uyarı</h6><h3 class="mb-0">{{ $openAlertCount }}</h3></div></div>
        </div>
        <div class="col-xl-3 col-sm-6">
            <div class="card"><div class="card-body"><h6 class="text-muted">Aktif Kural</h6><h3 class="mb-0">{{ $rules->where('is_active', true)->count() }}</h3></div></div>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-responsive-md align-middle">
                    <thead>
                        <tr>
                            <th>Kural</th>
                            <th>Müşteri</th>
                            <th>Platform</th>
                            <th>Koşul</th>
                            <th>Son Durum</th>
                            <th>Açık</th>
                            <th>Önem</th>
                            <th class="text-end">İşlem</th>
                        </tr>
                    </thead>
                    <tbody>
                    @forelse($rules as $rule)
                        <tr>
                            <td>
                                <strong>{{ $rule->name }}</strong>
                                <div class="text-muted fs-12">Son kontrol: {{ $rule->last_evaluated_at?->format('d.m.Y H:i') ?: '-' }}</div>
                            </td>
                            <td>{{ $rule->client?->company_name ?: 'Tüm müşteriler' }}</td>
                            <td>{{ strtoupper(str_replace('_',' ', $rule->platform)) }}</td>
                            <td>
                                <code>{{ $rule->metric }}</code> {{ $rule->operator }} {{ rtrim(rtrim(number_format((float) $rule->threshold, 4, ',', '.'), '0'), ',') }}
                                <div class="text-muted fs-12">{{ ucfirst($rule->period) }} / cooldown {{ $rule->cooldown_minutes }} dk</div>
                            </td>
                            <td>
                                @if($rule->last_status === 'triggered')
                                    <span class="badge badge-danger">Tetiklendi</span>
                                @elseif($rule->last_status === 'cooldown')
                                    <span class="badge badge-warning">Beklemede</span>
                                @elseif($rule->last_status === 'ok')
                                    <span class="badge badge-success">Normal</span>
                                @else
                                    <span class="badge badge-light">Çalışmadı</span>
                                @endif
                                <div class="text-muted fs-12">Son değer: {{ $rule->last_value !== null ? rtrim(rtrim(number_format((float) $rule->last_value, 4, ',', '.'), '0'), ',') : '-' }}</div>
                            </td>
                            <td>{{ $rule->open_logs_count }}</td>
                            <td>{{ ucfirst($rule->severity) }}</td>
                            <td class="text-end">
                                <form method="POST" action="{{ route('admin.alert-rules.evaluate', $rule) }}" class="d-inline">@csrf<button class="btn btn-xs btn-outline-warning">Çalıştır</button></form>
                                <a class="btn btn-xs btn-outline-info" href="{{ route('admin.alert-rules.show', $rule) }}">Detay</a>
                                <a class="btn btn-xs btn-outline-primary" href="{{ route('admin.alert-rules.edit', $rule) }}">Düzenle</a>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="8" class="text-center text-muted py-5">Henüz uyarı kuralı yok.</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            {{ $rules->links() }}
        </div>
    </div>
</div>
@endsection
