<?php

namespace App\Filament\Widgets\Config;

use App\Models\BlogPost;
use App\Models\Document;
use App\Models\Page;
use App\Models\Pet;
use App\Models\Setting;
use App\Models\User;
use Filament\Widgets\Widget;
use Illuminate\Support\Collection;

class RecentActivityWidget extends Widget
{
    protected string $view = 'filament.widgets.config.recent-activity';

    protected int|string|array $columnSpan = 'full';

    protected static ?int $sort = 4;

    /**
     * @return Collection<int, array{type: string, icon: string, color: string, title: string, description: string, time: string, url: string|null}>
     */
    public function getActivities(): Collection
    {
        $activities = collect();

        // Recent pets
        Pet::query()
            ->latest('updated_at')
            ->limit(3)
            ->get()
            ->each(function (Pet $pet) use ($activities) {
                $activities->push([
                    'type' => 'pet',
                    'icon' => 'heroicon-o-heart',
                    'color' => 'primary',
                    'title' => $pet->name,
                    'description' => $pet->wasRecentlyCreated || $pet->created_at->eq($pet->updated_at)
                        ? 'New pet added'
                        : 'Pet updated',
                    'time' => $pet->updated_at,
                    'url' => route('filament.admin.resources.pets.edit', $pet),
                ]);
            });

        // Recent pages
        Page::query()
            ->latest('updated_at')
            ->limit(2)
            ->get()
            ->each(function (Page $page) use ($activities) {
                $activities->push([
                    'type' => 'page',
                    'icon' => 'heroicon-o-document-text',
                    'color' => 'info',
                    'title' => $page->title,
                    'description' => $page->wasRecentlyCreated || $page->created_at->eq($page->updated_at)
                        ? 'New page created'
                        : 'Page updated',
                    'time' => $page->updated_at,
                    'url' => route('filament.config.resources.pages.edit', $page),
                ]);
            });

        // Recent blog posts
        BlogPost::query()
            ->latest('updated_at')
            ->limit(2)
            ->get()
            ->each(function (BlogPost $post) use ($activities) {
                $activities->push([
                    'type' => 'blog',
                    'icon' => 'heroicon-o-newspaper',
                    'color' => 'success',
                    'title' => $post->title,
                    'description' => $post->wasRecentlyCreated || $post->created_at->eq($post->updated_at)
                        ? 'New blog post'
                        : 'Blog post updated',
                    'time' => $post->updated_at,
                    'url' => null,
                ]);
            });

        // Recent documents
        Document::query()
            ->latest('updated_at')
            ->limit(2)
            ->get()
            ->each(function (Document $doc) use ($activities) {
                $activities->push([
                    'type' => 'document',
                    'icon' => 'heroicon-o-document-duplicate',
                    'color' => 'warning',
                    'title' => $doc->name,
                    'description' => $doc->wasRecentlyCreated || $doc->created_at->eq($doc->updated_at)
                        ? 'New document'
                        : 'Document updated',
                    'time' => $doc->updated_at,
                    'url' => route('filament.config.resources.documents.edit', $doc),
                ]);
            });

        // Recent users
        User::query()
            ->latest('created_at')
            ->limit(2)
            ->get()
            ->each(function (User $user) use ($activities) {
                $activities->push([
                    'type' => 'user',
                    'icon' => 'heroicon-o-user-plus',
                    'color' => 'gray',
                    'title' => $user->name,
                    'description' => 'New user registered',
                    'time' => $user->created_at,
                    'url' => route('filament.config.resources.users.edit', $user),
                ]);
            });

        // Recent settings changes
        Setting::query()
            ->latest('updated_at')
            ->limit(2)
            ->get()
            ->each(function (Setting $setting) use ($activities) {
                $activities->push([
                    'type' => 'setting',
                    'icon' => 'heroicon-o-cog-6-tooth',
                    'color' => 'danger',
                    'title' => str($setting->key)->title()->replace('_', ' ')->toString(),
                    'description' => 'Setting updated',
                    'time' => $setting->updated_at,
                    'url' => null,
                ]);
            });

        // Rows written by insertOrIgnore in older migrations can carry a null
        // timestamp, which would otherwise fatal on diffForHumans() and take
        // the whole dashboard down for a cosmetic column.
        return $activities
            ->reject(fn (array $activity): bool => $activity['time'] === null)
            ->sortByDesc('time')
            ->take(8)
            ->map(function ($activity) {
                $activity['time'] = $activity['time']->diffForHumans();

                return $activity;
            })
            ->values();
    }
}
