import { lazy, Suspense, useEffect } from 'react';
import { getConfig } from './config/env';
import { AppShell } from './components/AppShell';
import { SikshyaDialogProvider } from './components/shared/SikshyaDialogContext';
import { ShellStateProvider, useShellState } from './context/ShellStateContext';
import { AdminRoutingProvider, parseAdminRoute, useAdminRouting } from './lib/adminRouting';
import { applyAdminBrandThemeToRoot, clearAdminBrandThemeFromRoot } from './lib/adminBrandTokens';
import { term } from './lib/terminology';
import { __, sprintf } from './lib/i18n';
import type { NavItem } from './types';

const ActivityLogPage = lazy(() =>
  import('./pages/ActivityLogPage').then((m) => ({ default: m.ActivityLogPage }))
);
const AddonsPage = lazy(() => import('./pages/AddonsPage').then((m) => ({ default: m.AddonsPage })));
const BundlesPage = lazy(() => import('./pages/BundlesPage').then((m) => ({ default: m.BundlesPage })));
const CalendarPage = lazy(() => import('./pages/CalendarPage').then((m) => ({ default: m.CalendarPage })));
const ContentDripPage = lazy(() =>
  import('./pages/ContentDripPage').then((m) => ({ default: m.ContentDripPage }))
);
const ContentPostEditorPage = lazy(() =>
  import('./pages/ContentPostEditorPage').then((m) => ({ default: m.ContentPostEditorPage }))
);
const CouponsPage = lazy(() => import('./pages/CouponsPage').then((m) => ({ default: m.CouponsPage })));
const DiscussionsPage = lazy(() =>
  import('./pages/DiscussionsPage').then((m) => ({ default: m.DiscussionsPage }))
);
const CourseBuilderPage = lazy(() =>
  import('./pages/CourseBuilderPage').then((m) => ({ default: m.CourseBuilderPage }))
);
const CourseCategoriesPage = lazy(() =>
  import('./pages/CourseCategoriesPage').then((m) => ({ default: m.CourseCategoriesPage }))
);
const CourseTeamPage = lazy(() =>
  import('./pages/CourseTeamPage').then((m) => ({ default: m.CourseTeamPage }))
);
const CoursesPage = lazy(() => import('./pages/CoursesPage').then((m) => ({ default: m.CoursesPage })));
const DashboardPage = lazy(() =>
  import('./pages/DashboardPage').then((m) => ({ default: m.DashboardPage }))
);
const EmailMarketingPage = lazy(() =>
  import('./pages/EmailMarketingPage').then((m) => ({ default: m.EmailMarketingPage }))
);
const EmailPage = lazy(() => import('./pages/EmailPage').then((m) => ({ default: m.EmailPage })));
const EmailTemplateEditPage = lazy(() =>
  import('./pages/EmailTemplateEditPage').then((m) => ({ default: m.EmailTemplateEditPage }))
);
const EnrollmentsPage = lazy(() =>
  import('./pages/EnrollmentsPage').then((m) => ({ default: m.EnrollmentsPage }))
);
const EnrollmentDetailsPage = lazy(() =>
  import('./pages/EnrollmentDetailsPage').then((m) => ({ default: m.EnrollmentDetailsPage }))
);
const GenericPlaceholderPage = lazy(() =>
  import('./pages/GenericPlaceholderPage').then((m) => ({ default: m.GenericPlaceholderPage }))
);
const GradebookPage = lazy(() =>
  import('./pages/GradebookPage').then((m) => ({ default: m.GradebookPage }))
);
const GradingPage = lazy(() => import('./pages/GradingPage').then((m) => ({ default: m.GradingPage })));
const InstructorApplicationsPage = lazy(() =>
  import('./pages/InstructorApplicationsPage').then((m) => ({ default: m.InstructorApplicationsPage }))
);
const IntegrationsPage = lazy(() =>
  import('./pages/IntegrationsPage').then((m) => ({ default: m.IntegrationsPage }))
);
const IssuedCertificatesPage = lazy(() =>
  import('./pages/IssuedCertificatesPage').then((m) => ({ default: m.IssuedCertificatesPage }))
);
const LicensePage = lazy(() => import('./pages/LicensePage').then((m) => ({ default: m.LicensePage })));
const MarketplacePage = lazy(() =>
  import('./pages/MarketplacePage').then((m) => ({ default: m.MarketplacePage }))
);
const OrdersPage = lazy(() => import('./pages/OrdersPage').then((m) => ({ default: m.OrdersPage })));
const OrderDetailsPage = lazy(() =>
  import('./pages/OrderDetailsPage').then((m) => ({ default: m.OrderDetailsPage }))
);
const PaymentsPage = lazy(() => import('./pages/PaymentsPage').then((m) => ({ default: m.PaymentsPage })));
const PaymentDetailsPage = lazy(() =>
  import('./pages/PaymentDetailsPage').then((m) => ({ default: m.PaymentDetailsPage }))
);
const PrerequisitesPage = lazy(() =>
  import('./pages/PrerequisitesPage').then((m) => ({ default: m.PrerequisitesPage }))
);
const ReviewsPage = lazy(() => import('./pages/ReviewsPage').then((m) => ({ default: m.ReviewsPage })));
const ReviewDetailPage = lazy(() =>
  import('./pages/ReviewDetailPage').then((m) => ({ default: m.ReviewDetailPage }))
);
const SettingsPage = lazy(() => import('./pages/SettingsPage').then((m) => ({ default: m.SettingsPage })));
const SocialLoginPage = lazy(() =>
  import('./pages/SocialLoginPage').then((m) => ({ default: m.SocialLoginPage }))
);
const SubscriptionsProPage = lazy(() =>
  import('./pages/SubscriptionsProPage').then((m) => ({ default: m.SubscriptionsProPage }))
);
const WhiteLabelPage = lazy(() =>
  import('./pages/WhiteLabelPage').then((m) => ({ default: m.WhiteLabelPage }))
);
const WpEntityListPage = lazy(() =>
  import('./pages/WpEntityListPage').then((m) => ({ default: m.WpEntityListPage }))
);
const WpUserListPage = lazy(() =>
  import('./pages/WpUserListPage').then((m) => ({ default: m.WpUserListPage }))
);

const hubPages = () => import('./pages/hubs/HubPages');
const BrandingHubPage = lazy(() => hubPages().then((m) => ({ default: m.BrandingHubPage })));
const CertificatesHubPage = lazy(() => hubPages().then((m) => ({ default: m.CertificatesHubPage })));
const ContentLibraryHubPage = lazy(() => hubPages().then((m) => ({ default: m.ContentLibraryHubPage })));
const EmailHubPage = lazy(() => hubPages().then((m) => ({ default: m.EmailHubPage })));
const IntegrationsHubPage = lazy(() => hubPages().then((m) => ({ default: m.IntegrationsHubPage })));
const LearningRulesHubPage = lazy(() => hubPages().then((m) => ({ default: m.LearningRulesHubPage })));
const PeopleHubPage = lazy(() => hubPages().then((m) => ({ default: m.PeopleHubPage })));
const ReportsHubPage = lazy(() => hubPages().then((m) => ({ default: m.ReportsHubPage })));
const SalesHubPage = lazy(() => hubPages().then((m) => ({ default: m.SalesHubPage })));
const ToolsHubPage = lazy(() => hubPages().then((m) => ({ default: m.ToolsHubPage })));

/** Same splash as PHP `ReactAdminView` boot markup — one loader for pre-React + lazy chunks. */
function AdminRouteFallback() {
  return (
    <div
      className="sikshya-admin-boot-loader sikshya-admin-boot-loader--in-shell"
      role="status"
      aria-busy="true"
      aria-live="polite"
    >
      <span className="sikshya-admin-boot-spinner" aria-hidden />
      <span className="screen-reader-text">{__('Loading Sikshya…', 'sikshya')}</span>
    </div>
  );
}

function prefetchAdminChunks(): void {
  // Reduce sidebar "flicker" by preloading common route chunks shortly after first paint.
  // This avoids Suspense fallback flashes on each view change in slower environments.
  const w = window as unknown as { requestIdleCallback?: (cb: () => void, opts?: { timeout: number }) => void };
  const schedule = (cb: () => void) => {
    if (typeof w.requestIdleCallback === 'function') {
      w.requestIdleCallback(cb, { timeout: 1200 });
    } else {
      setTimeout(cb, 350);
    }
  };

  schedule(() => {
    void import('./pages/DashboardPage');
    void import('./pages/CoursesPage');
    void import('./pages/SettingsPage');
    void import('./pages/OrdersPage');
    void import('./pages/OrderDetailsPage');
    void import('./pages/PaymentsPage');
    void import('./pages/PaymentDetailsPage');
    void import('./pages/EnrollmentsPage');
    void import('./pages/EnrollmentDetailsPage');
    void import('./pages/AddonsPage');
    void import('./pages/EmailPage');
  });
}

function RoutedApp() {
  const baseConfig = getConfig();
  const { route } = useAdminRouting();
  const { navigation } = useShellState();
  const pageKey =
    typeof route.page === 'string' && route.page.trim() !== '' ? route.page.trim() : 'dashboard';
  const config = { ...baseConfig, page: pageKey, query: route.query ?? {}, navigation };
  const page = config.page;
  const q = (config.query || {}) as Record<string, unknown>;
  const isCertificateBuilder = page === 'edit-content' && String(q.post_type || '').trim() === 'sikshya_certificate';
  const platformName = config.branding?.pluginName?.trim() || 'Sikshya';

  useEffect(() => {
    const root = document.getElementById('sikshya-admin-root');
    if (!root) {
      return;
    }
    const b = config.branding;
    if (!b?.topbarBg && !b?.sidebarBg) {
      clearAdminBrandThemeFromRoot(root);
      return;
    }
    applyAdminBrandThemeToRoot(root, b);
    return () => {
      clearAdminBrandThemeFromRoot(root);
    };
  }, [config.branding?.topbarBg, config.branding?.sidebarBg]);
  const T = {
    course: term(config, 'course'),
    courses: term(config, 'courses'),
    lesson: term(config, 'lesson'),
    lessons: term(config, 'lessons'),
    quiz: term(config, 'quiz'),
    quizzes: term(config, 'quizzes'),
    assignment: term(config, 'assignment'),
    assignments: term(config, 'assignments'),
    chapter: term(config, 'chapter'),
    chapters: term(config, 'chapters'),
    student: term(config, 'student'),
    students: term(config, 'students'),
    instructor: term(config, 'instructor'),
    instructors: term(config, 'instructors'),
    enrollment: term(config, 'enrollment'),
    enrollments: term(config, 'enrollments'),
  };

  useEffect(() => {
    prefetchAdminChunks();
  }, []);

  const routes = (() => {
  switch (page) {
    case 'dashboard':
      return <DashboardPage embedded config={config} title={__('Dashboard', 'sikshya')} />;
    case 'courses':
      return <CoursesPage embedded config={config} title={T.courses} restBase="sik_course" />;
    case 'add-course':
      return <CourseBuilderPage embedded config={config} title={sprintf(__('%s builder', 'sikshya'), T.course)} />;
    case 'bundle-builder':
      return <CourseBuilderPage embedded config={config} title={__('Bundle builder', 'sikshya')} />;
    case 'edit-content':
      return <ContentPostEditorPage embedded config={config} shellTitle={__('Edit content', 'sikshya')} />;
    case 'lessons':
    case 'add-lesson':
      return (
        <WpEntityListPage
          config={config}
          title={T.lessons}
          subtitle={sprintf(__('All %s', 'sikshya'), T.lessons.toLowerCase())}
          restBase="sik_lesson"
        />
      );
    case 'quizzes':
      return (
        <WpEntityListPage
          config={config}
          title={T.quizzes}
          subtitle={sprintf(__('All %s', 'sikshya'), T.quizzes.toLowerCase())}
          restBase="sik_quiz"
        />
      );
    case 'assignments':
      return (
        <WpEntityListPage
          config={config}
          title={T.assignments}
          subtitle={sprintf(__('All %s', 'sikshya'), T.assignments.toLowerCase())}
          restBase="sik_assignment"
        />
      );
    case 'questions':
      return (
        <WpEntityListPage
          config={config}
          title={__('Questions', 'sikshya')}
          subtitle={__('All questions', 'sikshya')}
          restBase="sik_question"
        />
      );
    case 'chapters':
      return (
        <WpEntityListPage
          config={config}
          title={__('Chapters', 'sikshya')}
          subtitle={sprintf(__('All %s', 'sikshya'), T.chapters.toLowerCase())}
          restBase="sik_chapter"
        />
      );
    case 'certificates':
      return (
        <WpEntityListPage
          config={config}
          title={__('Certificates', 'sikshya')}
          subtitle={__('Certificate templates', 'sikshya')}
          restBase="sikshya_certificate"
        />
      );
    case 'issued-certificates':
      return <IssuedCertificatesPage embedded config={config} title={__('Issued certificates', 'sikshya')} />;
    case 'orders':
      return <OrdersPage embedded config={config} title={__('Orders', 'sikshya')} />;
    case 'order':
      return <OrderDetailsPage embedded config={config} title={__('Order', 'sikshya')} />;
    case 'coupons':
      return <CouponsPage embedded config={config} title={__('Coupons', 'sikshya')} />;
    case 'reviews':
      return <ReviewsPage embedded config={config} title={__('Course reviews', 'sikshya')} />;
    case 'review':
      return <ReviewDetailPage embedded config={config} title={__('Review details', 'sikshya')} />;
    case 'discussions':
      return <DiscussionsPage embedded config={config} title={__('Discussions & Q&A', 'sikshya')} />;
    case 'gradebook':
      return <GradebookPage embedded config={config} title={__('Gradebook', 'sikshya')} uiMode="default" />;
    case 'assignment-submissions':
      return (
        <GradebookPage embedded config={config} title={__('Assignment submissions', 'sikshya')} uiMode="submissions" />
      );
    case 'grading':
      return <GradingPage embedded config={config} title={__('Grading', 'sikshya')} />;
    case 'activity-log':
      return <ActivityLogPage embedded config={config} title={__('Activity log', 'sikshya')} />;
    case 'content-drip':
      return <ContentDripPage embedded config={config} title={__('Scheduled access', 'sikshya')} />;
    case 'subscriptions':
      return <SubscriptionsProPage embedded config={config} title={__('Subscriptions', 'sikshya')} />;
    case 'course-team':
      return <CourseTeamPage embedded config={config} title={__('Course staff', 'sikshya')} />;
    case 'marketplace':
      return <MarketplacePage embedded config={config} title={__('Marketplace', 'sikshya')} />;
    case 'bundles':
      return <BundlesPage embedded config={config} title={__('Course bundles', 'sikshya')} />;
    case 'prerequisites':
      return <PrerequisitesPage embedded config={config} title={__('Prerequisites', 'sikshya')} />;
    case 'social-login':
      return <SocialLoginPage embedded config={config} title={__('Social login', 'sikshya')} />;
    case 'white-label':
      return <WhiteLabelPage embedded config={config} title={__('White label', 'sikshya')} />;
    case 'crm-automation':
      return (
        <GenericPlaceholderPage
          config={config}
          title={__('CRM & email automation', 'sikshya')}
          description={__('This screen is not wired in the React shell yet.', 'sikshya')}
        />
      );
    case 'calendar':
      return <CalendarPage embedded config={config} title={__('Calendar', 'sikshya')} />;
    /* ---- Tabbed hubs (new sidebar entries that fan out to existing pages). ---- */
    case 'content-library':
      return <ContentLibraryHubPage embedded config={config} title={__('Content library', 'sikshya')} />;
    case 'people':
      return <PeopleHubPage embedded config={config} title={__('People', 'sikshya')} />;
    case 'certificates-hub':
      return <CertificatesHubPage embedded config={config} title={__('Certificates', 'sikshya')} />;
    case 'sales':
      return <SalesHubPage embedded config={config} title={__('Sales', 'sikshya')} />;
    case 'email-hub':
    case 'email-templates':
      return <EmailHubPage embedded config={config} title={__('Email', 'sikshya')} />;
    case 'branding':
      return <BrandingHubPage embedded config={config} title={__('Branding', 'sikshya')} />;
    case 'integrations-hub':
      return <IntegrationsHubPage embedded config={config} title={__('Integrations', 'sikshya')} />;
    case 'learning-rules':
      return <LearningRulesHubPage embedded config={config} title={__('Learning rules', 'sikshya')} />;
    case 'course-categories':
      return (
        <CourseCategoriesPage
          embedded
          config={config}
          title={__('Course categories', 'sikshya')}
          subtitle={__('Organize courses with hierarchical categories.', 'sikshya')}
        />
      );
    case 'students':
      return (
        <WpUserListPage
          config={config}
          title={T.students}
          subtitle={sprintf(__('Users with the %s role.', 'sikshya'), T.student)}
          variant="students"
        />
      );
    case 'instructors':
      return (
        <WpUserListPage
          config={config}
          title={T.instructors}
          subtitle={sprintf(
            __('Users with the %s role. Pending sign-ups are under People → Applications.', 'sikshya'),
            T.instructor
          )}
          variant="instructors"
        />
      );
    case 'instructor-applications':
      return (
        <InstructorApplicationsPage
          embedded
          config={config}
          title={__('Instructor applications', 'sikshya')}
          subtitle={__('Approve or reject learners who applied to teach.', 'sikshya')}
        />
      );
    case 'enrollments':
      return <EnrollmentsPage embedded config={config} title={T.enrollments} />;
    case 'enrollment-detail':
      return <EnrollmentDetailsPage embedded config={config} title={__('Enrollment', 'sikshya')} />;
    case 'reports':
      return <ReportsHubPage embedded config={config} title={__('Reports', 'sikshya')} />;
    case 'payments':
      return <PaymentsPage embedded config={config} title={__('Payments', 'sikshya')} />;
    case 'payment':
      return <PaymentDetailsPage embedded config={config} title={__('Payment', 'sikshya')} />;
    case 'settings':
      return <SettingsPage embedded config={config} title={__('Settings', 'sikshya')} />;
    case 'email':
      return <EmailPage embedded config={config} title={__('Email', 'sikshya')} />;
    case 'email-template-edit':
      return <EmailTemplateEditPage embedded config={config} title={__('Email template', 'sikshya')} />;
    case 'tools':
      return <ToolsHubPage embedded config={config} title={__('Tools', 'sikshya')} />;
    case 'addons':
      return <AddonsPage embedded config={config} title={__('Addons', 'sikshya')} />;
    case 'integrations':
      return <IntegrationsPage embedded config={config} title={__('Integrations', 'sikshya')} />;
    case 'email-marketing':
      return <EmailMarketingPage embedded config={config} title={__('Email marketing', 'sikshya')} />;
    case 'license':
      return <LicensePage embedded config={config} title={__('License', 'sikshya')} />;
    default:
      return (
        <GenericPlaceholderPage
          config={config}
          title={platformName}
          description={__('This admin screen is powered by the React shell.', 'sikshya')}
        />
      );
  }
  })();

  const navTitle = (() => {
    const items = config.navigation as NavItem[];
    const walk = (rows: NavItem[] | undefined): string | null => {
      if (!Array.isArray(rows)) return null;
      for (const r of rows) {
        if ((r as any)?.id === page && typeof (r as any)?.label === 'string' && (r as any).label.trim() !== '') {
          return String((r as any).label).trim();
        }
        const kids = (r as any)?.children as NavItem[] | undefined;
        const hit = walk(kids);
        if (hit) return hit;
      }
      return null;
    };
    return walk(items);
  })();

  const shellTitle = (() => {
    if (navTitle) return navTitle;
    if (page === 'dashboard') return __('Dashboard', 'sikshya');
    if (page === 'settings') return __('Settings', 'sikshya');
    if (page === 'courses') return T.courses;
    if (page === 'add-course') return sprintf(__('%s builder', 'sikshya'), T.course);
    if (page === 'bundle-builder') return __('Bundle builder', 'sikshya');
    if (page === 'edit-content') {
      const postType = String(q.post_type || '').trim();
      const id = Number(q.post_id || q.id || 0) || 0;
      const label =
        postType === 'sikshya_certificate'
          ? __('Certificate', 'sikshya')
          : postType === 'sik_lesson'
            ? T.lesson
            : postType === 'sik_quiz'
              ? T.quiz
              : postType === 'sik_assignment'
                ? T.assignment
                : postType === 'sik_course'
                  ? T.course
                  : postType
                    ? postType
                    : __('Content', 'sikshya');
      return id > 0 ? sprintf(__('Edit %s', 'sikshya'), label) : sprintf(__('New %s', 'sikshya'), label);
    }
    return platformName;
  })();

  // Certificate builder is a full-screen workspace with its own header/actions.
  // Do not wrap it in the admin shell (no sidebar, no top bar).
  // No Suspense fallback: PHP skips the Sikshya boot loader for this route; hosts often show their own loader first.
  if (isCertificateBuilder) {
    return <Suspense fallback={null}>{routes}</Suspense>;
  }

  return (
    <AppShell
      page={config.page}
      version={config.version}
      navigation={config.navigation as NavItem[]}
      adminUrl={config.adminUrl}
      pluginUrl={config.pluginUrl}
      user={config.user}
      branding={config.branding}
      title={shellTitle}
      subtitle={page === 'settings' ? __('Site-wide defaults for every course', 'sikshya') : undefined}
    >
      <Suspense fallback={<AdminRouteFallback />}>{routes}</Suspense>
    </AppShell>
  );
}

export default function App() {
  const baseConfig = getConfig();
  // Ensure the initial render respects the URL (deep links / reload).
  // This also protects us if PHP-provided config.page lags behind the URL.
  const initial = parseAdminRoute(baseConfig);
  const normalizedBase = { ...baseConfig, page: initial.page, query: initial.query };

  return (
    <AdminRoutingProvider baseConfig={normalizedBase}>
      <ShellStateProvider>
        <SikshyaDialogProvider>
          <RoutedApp />
        </SikshyaDialogProvider>
      </ShellStateProvider>
    </AdminRoutingProvider>
  );
}
