<?php

namespace Functional\Streaks\Providers;

use Functional\Streaks\Console\Commands\RemindStreaksCommand;
use Functional\Streaks\Database\Seeders\StreaksSeeder;
use Functional\Streaks\Listeners\UpdateStreakOnWorkoutCompleted;
use Functional\Streaks\Services\StreakService;
use Functional\Tracking\Events\WorkoutCompleted;
use Illuminate\Support\Facades\Event;
use Xefi\LaravelOSDD\LayerServiceProvider;

class StreaksServiceProvider extends LayerServiceProvider
{
    public function boot(): void
    {
        if ($this->app->runningInConsole()) {
            $this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');
            $this->loadSeeders([StreaksSeeder::class]);
            $this->commands([RemindStreaksCommand::class]);
        }

        $this->withRouting(
            web: __DIR__ . '/../../routes/web.php',
            api: __DIR__ . '/../../routes/api.php',
            commands: __DIR__ . '/../../routes/console.php',
            channels: __DIR__ . '/../../routes/channels.php',
        );

        // Le streak se met à jour à chaque séance terminée (événement porté par la couche tracking).
        Event::listen(WorkoutCompleted::class, UpdateStreakOnWorkoutCompleted::class);
    }

    public function register(): void
    {
        $this->app->singleton(StreakService::class);
    }
}
