<?php

namespace App\Filament\Widgets;

use App\Models\BoardingHouse;
use App\Models\Facility;
use App\Models\Room;
use App\Models\Transaction;
use App\Models\User;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Illuminate\Support\Facades\Auth;

use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;

class StatsOverview extends BaseWidget
{

    // 2. TAMBAHKAN BARIS INI DI DALAM CLASS
    use HasWidgetShield;
    // Mengatur agar widget tampil full width
    protected int | string | array $columnSpan = 'full';

    protected function getStats(): array
    {
        $user = Auth::user();

        // ==========================================
        // 1. TAMPILAN UNTUK ADMIN & SUPER ADMIN
        // ==========================================
        if ($user->hasRole(['super_admin', 'Admin'])) {
            return [
                Stat::make('Total Properti Kos', BoardingHouse::where('category', 'kos')->count())
                    ->description('Semua kos terdaftar')
                    ->descriptionIcon('heroicon-m-home-modern')
                    ->color('success'),

                Stat::make('Total Properti Kontrakan', BoardingHouse::where('category', 'kontrakan')->count())
                    ->description('Semua kontrakan terdaftar')
                    ->descriptionIcon('heroicon-m-home')
                    ->color('info'),

                Stat::make('Total Kamar', Room::count())
                    ->description('Kamar dari seluruh properti')
                    ->descriptionIcon('heroicon-m-squares-2x2')
                    ->color('success'),

                Stat::make('Total Fasilitas', Facility::count())
                    ->description('Master data fasilitas')
                    ->descriptionIcon('heroicon-m-list-bullet')
                    ->color('warning'),

                // Perbaikan Icon: Menggunakan briefcase untuk Owner
                Stat::make('Total Owner', User::role('Owner')->count())
                    ->description('Mitra properti bergabung')
                    ->descriptionIcon('heroicon-m-briefcase')
                    ->color('primary'),

                Stat::make('Total Tenant', User::role('Tenant')->count())
                    ->description('Penyewa terdaftar')
                    ->descriptionIcon('heroicon-m-users')
                    ->color('primary'),

                Stat::make('Transaksi Berhasil', Transaction::where('survey_status', 'like', '%DEAL%')->count())
                    ->description('Total transaksi Deal')
                    ->descriptionIcon('heroicon-m-check-badge')
                    ->color('success'),

                Stat::make('Total Booking', Transaction::count())
                    ->description('Semua riwayat pemesanan')
                    ->descriptionIcon('heroicon-m-document-text')
                    ->color('gray'),
            ];
        }

        // ==========================================
        // 2. TAMPILAN UNTUK OWNER
        // ==========================================

        $ownerPropertyIds = BoardingHouse::where('user_id', $user->id)->pluck('id');

        return [
            Stat::make('Properti Kos Saya', BoardingHouse::where('user_id', $user->id)->where('category', 'kos')->count())
                ->description('Kos yang Anda kelola')
                ->descriptionIcon('heroicon-m-home-modern')
                ->color('success'),

            Stat::make('Kontrakan Saya', BoardingHouse::where('user_id', $user->id)->where('category', 'kontrakan')->count())
                ->description('Kontrakan yang Anda kelola')
                ->descriptionIcon('heroicon-m-home')
                ->color('info'),

            Stat::make('Kamar Tersedia', Room::whereIn('boarding_house_id', $ownerPropertyIds)->where('is_available', true)->count())
                ->description('Kamar siap disewakan')
                ->descriptionIcon('heroicon-m-check-circle')
                ->color('success'),

            Stat::make('Kamar Terisi', Room::whereIn('boarding_house_id', $ownerPropertyIds)->where('is_available', false)->count())
                ->description('Kamar sedang disewa')
                ->descriptionIcon('heroicon-m-x-circle')
                ->color('danger'),

            Stat::make('Menunggu Diproses', Transaction::whereHas('room', function ($query) use ($ownerPropertyIds) {
                $query->whereIn('boarding_house_id', $ownerPropertyIds);
            })->where('survey_status', 'like', '%pending%')->count())
                ->description('Survei/Booking baru')
                ->descriptionIcon('heroicon-m-bell-alert')
                ->color('warning'),

            Stat::make('Transaksi Sukses', Transaction::whereHas('room', function ($query) use ($ownerPropertyIds) {
                $query->whereIn('boarding_house_id', $ownerPropertyIds);
            })->where('survey_status', 'like', '%DEAL%')->count())
                ->description('Riwayat sewa Deal')
                ->descriptionIcon('heroicon-m-banknotes')
                ->color('success'),
        ];
    }
}
