<?php

namespace App\Filament\Support;

use App\Enums\NotifStyle;
use Filament\Notifications\Notification;

class CheerfulNotification
{
    /**
     * Create a new notification instance.
     */
    public static function make(): Notification
    {
        return Notification::make();
    }

    /**
     * Get the current user's Notification style preference.
     */
    public static function getNotifStyle(): NotifStyle
    {
        try {
            return auth()->user()?->settings?->notif_style ?? NotifStyle::CHEERFUL;
        } catch (\Throwable $e) {
            return NotifStyle::CHEERFUL;
        }
    }

    public static function create(): Notification
    {
        return self::make()
            ->title(self::getByKey('create.title'))
            ->body(self::getByKey('create.body'))
            ->success();
    }

    public static function update(): Notification
    {
        return self::make()
            ->title(self::getByKey('update.title'))
            ->body(self::getByKey('update.body'))
            ->success();
    }

    public static function delete(): Notification
    {
        return self::make()
            ->title(self::getByKey('delete.title'))
            ->body(self::getByKey('delete.body'))
            ->success();
    }

    public static function forceDelete(): Notification
    {
        return self::make()
            ->title(self::getByKey('force_delete.title'))
            ->body(self::getByKey('force_delete.body'))
            ->success();
    }

    public static function restore(): Notification
    {
        return self::make()
            ->title(self::getByKey('restore.title'))
            ->body(self::getByKey('restore.body'))
            ->success();
    }

    public static function statusUpdated(?string $title = null, ?string $body = null): Notification
    {
        return self::make()
            ->title($title ?? self::getByKey('status_updated.title'))
            ->body($body ?? self::getByKey('status_updated.body'))
            ->success();
    }

    public static function bulkDelete(): Notification
    {
        return self::make()
            ->title(self::getByKey('bulk_delete.title'))
            ->body(self::getByKey('bulk_delete.body'))
            ->success();
    }

    public static function bulkForceDelete(): Notification
    {
        return self::make()
            ->title(self::getByKey('bulk_force_delete.title'))
            ->body(self::getByKey('bulk_force_delete.body'))
            ->success();
    }

    public static function bulkRestore(): Notification
    {
        return self::make()
            ->title(self::getByKey('bulk_restore.title'))
            ->body(self::getByKey('bulk_restore.body'))
            ->success();
    }

    /**
     * Custom cheerful success notification.
     */
    public static function success(string $title, string $body): Notification
    {
        return self::make()
            ->title($title)
            ->body($body)
            ->success();
    }

    /**
     * Custom cheerful info notification.
     */
    public static function info(string $title, string $body): Notification
    {
        return self::make()
            ->title($title)
            ->body($body)
            ->info();
    }

    /**
     * Custom cheerful warning notification.
     */
    public static function warning(string $title, string $body): Notification
    {
        return self::make()
            ->title($title)
            ->body($body)
            ->warning();
    }

    /**
     * Custom cheerful danger notification.
     */
    public static function danger(string $title, string $body): Notification
    {
        return self::make()
            ->title($title)
            ->body($body)
            ->danger();
    }

    /**
     * Get dynamic message based on UX style from language file.
     */
    public static function getByKey(?string $key = null, array $replace = []): string
    {
        if (! $key) {
            return '';
        }

        $style = self::getNotifStyle()->value;

        return __("notif.{$key}.{$style}", $replace);
    }
}
