<?php

namespace App\Core\Modules;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;
use App\Core\Registry\ModuleRegistry;

abstract class ModuleServiceProvider extends ServiceProvider
{
    /**
     * Module name
     */
    protected string $moduleName;

    /**
     * Module slug
     */
    protected string $moduleSlug;

    /**
     * Module path
     */
    protected string $modulePath;

    /**
     * Register module services
     */
    public function register(): void
    {
        $this->loadModuleMetadata();
        $this->registerModule();
        $this->registerConfig();
        $this->registerHelpers();
    }

    /**
     * Bootstrap module services
     */
    public function boot(): void
    {
        if ($this->app->runningInConsole()) {
            $this->registerMigrations();
            $this->registerPublishing();
        }

        $this->registerRoutes();
        $this->registerViews();
        $this->registerTranslations();
    }

    /**
     * Load module metadata from module.json
     */
    protected function loadModuleMetadata(): array
    {
        $metadataPath = $this->modulePath . '/module.json';

        if (file_exists($metadataPath)) {
            return json_decode(file_get_contents($metadataPath), true);
        }

        return [];
    }

    /**
     * Register module in the registry
     */
    protected function registerModule(): void
    {
        $metadata = $this->loadModuleMetadata();
        ModuleRegistry::register($this->moduleSlug, $metadata);
    }

    /**
     * Register module configuration
     */
    protected function registerConfig(): void
    {
        $configPath = $this->modulePath . '/Config/' . $this->moduleSlug . '.php';

        if (file_exists($configPath)) {
            $this->mergeConfigFrom($configPath, $this->moduleSlug);
        }
    }

    /**
     * Register module routes
     */
    protected function registerRoutes(): void
    {
        // Only load routes if module is enabled
        if($this->moduleSlug != 'settings'){
            if (!ModuleRegistry::enabled($this->moduleSlug)) {
                return;
            }
        }
        

        $webRoutePath = $this->modulePath . '/Routes/web.php';
        $apiRoutePath = $this->modulePath . '/Routes/api.php';

        if (file_exists($webRoutePath)) {
            Route::middleware('web')
                ->group($webRoutePath);
        }

        if (file_exists($apiRoutePath)) {
            Route::middleware('api')
                ->prefix('api')
                ->group($apiRoutePath);
        }
    }

    /**
     * Register module views
     */
    protected function registerViews(): void
    {
        $viewPath = $this->modulePath . '/Views';

        if (is_dir($viewPath)) {
            $this->loadViewsFrom($viewPath, $this->moduleSlug);
        }
    }

    /**
     * Register module migrations
     */
    protected function registerMigrations(): void
    {
        $migrationPath = $this->modulePath . '/Database/Migrations';

        if (is_dir($migrationPath)) {
            $this->loadMigrationsFrom($migrationPath);
        }
    }

    /**
     * Register module translations
     */
    protected function registerTranslations(): void
    {
        $langPath = $this->modulePath . '/Resources/lang';

        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, $this->moduleSlug);
        }
    }

    /**
     * Register publishing resources
     */
    protected function registerPublishing(): void
    {
        $configPath = $this->modulePath . '/Config/' . $this->moduleSlug . '.php';

        if (file_exists($configPath)) {
            $this->publishes([
                $configPath => config_path($this->moduleSlug . '.php'),
            ], $this->moduleSlug . '-config');
        }
    }

    /**
     * Register module helpers
     */
    protected function registerHelpers(): void
    {
        $helperPath = $this->modulePath . '/helpers.php';

        if (file_exists($helperPath)) {
            require_once $helperPath;
        }
    }
}
