<?php

namespace Modules\ClinicSite\Http\Controllers;

use Illuminate\Routing\Controller;

/**
 * Core ERP integration hooks for ClinicSite. Contributes its admin links into the
 * ClinicCore sidebar via clinicCoreMenuItems().
 */
class DataController extends Controller
{
    public function superadmin_package()
    {
        return [
            [
                'name'    => 'clinic_site',
                'label'   => __('clinicSite::lang.clinic_site_module'),
                'default' => false,
            ],
        ];
    }

    public function user_permissions()
    {
        return [
            ['value' => 'clinic_site.access_module',   'label' => 'Website: Access Module',          'default' => false],
            ['value' => 'clinic_site.manage_content',  'label' => 'Website: Manage Pages & Content',  'default' => false],
            ['value' => 'clinic_site.manage_design',   'label' => 'Website: Manage Theme & Design',   'default' => false],
            ['value' => 'clinic_site.manage_settings', 'label' => 'Website: Manage Settings & Domain','default' => false],
        ];
    }

    /** Sidebar contribution rendered INSIDE the ClinicCore "Extensions" dropdown. */
    public function clinicCoreMenuItems()
    {
        return function ($sub) {
            if (!module_entitled('ClinicSite')) return;
            $user = auth()->user();
            if (!$user || !$user->can('clinic_site.access_module')) {
                return;
            }

            $isSeg = fn($seg) => request()->segment(1) === 'clinic-site' && request()->segment(2) === $seg;

            $sub->dropdown(__('clinicSite::lang.website'), function ($s) use ($user, $isSeg) {
                $s->url(route('cs.dashboard'), __('clinicSite::lang.dashboard'),
                    ['active' => request()->segment(1) === 'clinic-site' && !request()->segment(2)]);

                if ($user->can('clinic_site.manage_content')) {
                    $s->url(route('cs.pages'), __('clinicSite::lang.pages'), ['active' => $isSeg('pages')]);
                    $s->url(route('cs.media'), __('clinicSite::lang.media'), ['active' => $isSeg('media')]);
                    $s->url(route('cs.navigation'), __('clinicSite::lang.navigation'), ['active' => $isSeg('navigation')]);
                }
                if ($user->can('clinic_site.manage_design')) {
                    $s->url(route('cs.theme'), __('clinicSite::lang.theme'), ['active' => $isSeg('theme')]);
                }
                $s->url(route('cs.messages'), __('clinicSite::lang.messages'), ['active' => $isSeg('messages')]);
                if ($user->can('clinic_site.manage_settings')) {
                    $s->url(route('cs.domain'), __('clinicSite::lang.domain_seo'), ['active' => $isSeg('domain')]);
                    $s->url(route('cs.settings'), __('clinicSite::lang.settings'), ['active' => $isSeg('settings')]);
                }
                $s->url(route('cs.preview'), __('clinicSite::lang.open_website'), ['active' => false]);
            }, ['icon' => 'fas fa-globe']);
        };
    }
}
