import AppLayout from '@/layout/AppLayout.vue';
import AuthService from '@/service/AuthService';
import IdentityService from '@/service/IdentityService';
import { createRouter, createWebHistory } from 'vue-router';

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            component: AppLayout,
            children: [
                {
                    path: '/',
                    name: 'dashboard',
                    component: () => import('@/views/DashboardNew.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/dashboard-new',
                    name: 'dashboard-new',
                    component: () => import('@/views/DashboardNew.vue'),
                    meta: { requiresAuth: true }
                },

                {
                    path: '/uikit/formlayout',
                    name: 'formlayout',
                    component: () => import('@/views/uikit/FormLayout.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/uikit/input',
                    name: 'input',
                    component: () => import('@/views/uikit/InputDoc.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/uikit/button',
                    name: 'button',
                    component: () => import('@/views/uikit/ButtonDoc.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/uikit/table',
                    name: 'table',
                    component: () => import('@/views/uikit/TableDoc.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/uikit/list',
                    name: 'list',
                    component: () => import('@/views/uikit/ListDoc.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/uikit/tree',
                    name: 'tree',
                    component: () => import('@/views/uikit/TreeDoc.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/uikit/panel',
                    name: 'panel',
                    component: () => import('@/views/uikit/PanelsDoc.vue'),
                    meta: { requiresAuth: true }
                },

                {
                    path: '/uikit/overlay',
                    name: 'overlay',
                    component: () => import('@/views/uikit/OverlayDoc.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/uikit/media',
                    name: 'media',
                    component: () => import('@/views/uikit/MediaDoc.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/uikit/message',
                    name: 'message',
                    component: () => import('@/views/uikit/MessagesDoc.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/uikit/file',
                    name: 'file',
                    component: () => import('@/views/uikit/FileDoc.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/uikit/menu',
                    name: 'menu',
                    component: () => import('@/views/uikit/MenuDoc.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/uikit/charts',
                    name: 'charts',
                    component: () => import('@/views/uikit/ChartDoc.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/uikit/misc',
                    name: 'misc',
                    component: () => import('@/views/uikit/MiscDoc.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/uikit/timeline',
                    name: 'timeline',
                    component: () => import('@/views/uikit/TimelineDoc.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/pages/empty',
                    name: 'empty',
                    component: () => import('@/views/pages/Empty.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/pages/crud',
                    name: 'crud',
                    component: () => import('@/views/pages/Crud.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/documentation',
                    name: 'documentation',
                    component: () => import('@/views/pages/Documentation.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/news',
                    name: 'news',
                    component: () => import('@/views/news/NewsList.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/news/dashboard',
                    name: 'news-dashboard',
                    component: () => import('@/views/news/NewsDashboard.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/news/:id',
                    name: 'news-detail',
                    component: () => import('@/views/news/NewsDetail.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/reports',
                    name: 'reports',
                    component: () => import('@/views/reports/ReportManager.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/reports/generator',
                    name: 'reports-generator',
                    component: () => import('@/views/reports/ReportGenerator.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/reports/history',
                    name: 'reports-history',
                    component: () => import('@/views/reports/ReportHistory.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/smm/campaigns',
                    name: 'smm-campaigns',
                    component: () => import('@/views/smm/CampaignList.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/smm/channels',
                    name: 'smm-channels',
                    component: () => import('@/views/smm/ChannelList.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/smm/content',
                    name: 'smm-content',
                    component: () => import('@/views/smm/ContentList.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/smm/analytics',
                    name: 'smm-analytics',
                    component: () => import('@/views/smm/AnalyticsDashboard.vue'),
                    meta: { requiresAuth: true }
                },
                {
                    path: '/smm',
                    name: 'smm-dashboard',
                    component: () => import('@/views/smm/SmmDashboard.vue'),
                    meta: { requiresAuth: true }
                }
            ]
        },
        {
            path: '/admin',
            component: AppLayout,
            children: [
                {
                    path: 'users',
                    name: 'admin-users',
                    component: () => import('@/views/pages/AdminUsers.vue'),
                    meta: { requiresAuth: true, requiresAdmin: true }
                },
                {
                    path: 'news',
                    name: 'admin-news',
                    component: () => import('@/views/news/NewsAdmin.vue'),
                    meta: { requiresAuth: true, requiresAdmin: true }
                }
            ]
        },
        {
            path: '/landing',
            name: 'landing',
            component: () => import('@/views/pages/Landing.vue')
        },
        {
            path: '/marketing-analysis',
            name: 'marketing-main',
            component: () => import('@/views/pages/marketing/MarketingMain.vue'),
            meta: { requiresAuth: true }
        },
        {
            path: '/marketing-analysis/analysis',
            name: 'marketing-analysis',
            component: () => import('@/views/pages/marketing/MarketingAnalysis.vue'),
            meta: { requiresAuth: true }
        },
        {
            path: '/marketing-analysis/promotion',
            name: 'marketing-promotion',
            component: () => import('@/views/pages/marketing/MarketingPromotion.vue'),
            meta: { requiresAuth: true }
        },
        {
            path: '/marketing-analysis/results',
            name: 'marketing-results',
            component: () => import('@/views/pages/marketing/MarketingResults.vue'),
            meta: { requiresAuth: true }
        },
        {
            path: '/marketing-analysis/analyses',
            name: 'marketing-analyses-list',
            component: () => import('@/views/pages/marketing/MarketingAnalysesList.vue'),
            meta: { requiresAuth: true }
        },
        {
            path: '/marketing-analysis/strategies',
            name: 'marketing-strategies-list',
            component: () => import('@/views/pages/marketing/MarketingStrategiesList.vue'),
            meta: { requiresAuth: true }
        },
        {
            path: '/marketing-analysis/credentials',
            name: 'marketing-credentials',
            component: () => import('@/views/pages/marketing/MarketingCredentials.vue'),
            meta: { requiresAuth: true }
        },
        {
            path: '/pages/notfound',
            name: 'notfound',
            component: () => import('@/views/pages/NotFound.vue')
        },

        {
            path: '/auth/login',
            name: 'login',
            component: () => import('@/views/pages/auth/Login.vue')
        },
        {
            path: '/auth/access',
            name: 'accessDenied',
            component: () => import('@/views/pages/auth/Access.vue')
        },
        {
            path: '/auth/error',
            name: 'error',
            component: () => import('@/views/pages/auth/Error.vue')
        }
    ]
});

router.beforeEach(async (to) => {
    const isAuthenticated = !!AuthService.getAccessToken();
    if (to.meta?.requiresAuth && !isAuthenticated) {
        return { name: 'login', query: { redirect: to.fullPath } };
    }
    if (to.name === 'login' && isAuthenticated) {
        return { path: '/' };
    }
    if (to.meta?.requiresAdmin) {
        try {
            const me = await IdentityService.getCurrentUser();
            const roles = Array.isArray(me?.roles) ? me.roles : [];
            if (!roles.includes('ROLE_ADMIN')) {
                return { name: 'accessDenied' };
            }
        } catch (_) {
            return { name: 'login', query: { redirect: to.fullPath } };
        }
    }
});

export default router;
