import { Routes } from '@angular/router';
// Core components (eagerly loaded - needed for initial navigation)
import { LogoutComponent } from './components/logout/logout.component';
import { NotFoundComponent } from './components/not-found/not-found.component';
import { TestSsrComponent } from './components/test-ssr/test-ssr.component';
import { EmailAuthorizedComponent } from './components/email-authorized/email-authorized.component';
import { EmailVerificationComponent } from './components/email-verification/email-verification.component';
import { LoginRegisterComponent } from './components/login-register/login-register.component';

// Guards
import { AuthGuard } from './guards/auth.guard';
import { ListingCategoryGuard } from './guards/listing-category.guard';

// Public pages - HomeComponent stays eagerly loaded as landing page
import { HomeComponent } from './pages/home/home.component';
import { SuccessComponent } from './pages/success/success.component';
import { TermsOfUseComponent } from './pages/legal/terms-of-use.component';
import { PrivacyStatementComponent } from './pages/legal/privacy-statement.component';

// Note: All listing and details components are now lazy-loaded for better initial bundle size


export const routes: Routes = [
  {
    path: 'test-ssr',
    component: TestSsrComponent,
  },
  {
    path: 'businessListings',
    loadComponent: () => import('./pages/listings/business-listings/business-listings.component').then(m => m.BusinessListingsComponent),
    runGuardsAndResolvers: 'always',
  },
  {
    path: 'commercialPropertyListings',
    loadComponent: () => import('./pages/listings/commercial-property-listings/commercial-property-listings.component').then(m => m.CommercialPropertyListingsComponent),
    runGuardsAndResolvers: 'always',
  },
  {
    path: 'brokerListings',
    loadComponent: () => import('./pages/listings/broker-listings/broker-listings.component').then(m => m.BrokerListingsComponent),
    runGuardsAndResolvers: 'always',
  },
  {
    path: 'home',
    component: HomeComponent,
  },
  // #########
  // Listings Details - New SEO-friendly slug-based URLs
  {
    path: 'business/:slug',
    loadComponent: () => import('./pages/details/details-business-listing/details-business-listing.component').then(m => m.DetailsBusinessListingComponent),
  },
  {
    path: 'commercial-property/:slug',
    loadComponent: () => import('./pages/details/details-commercial-property-listing/details-commercial-property-listing.component').then(m => m.DetailsCommercialPropertyListingComponent),
  },
  // Backward compatibility redirects for old UUID-based URLs
  {
    path: 'details-business-listing/:id',
    redirectTo: 'business/:id',
    pathMatch: 'full',
  },
  {
    path: 'details-commercial-property-listing/:id',
    redirectTo: 'commercial-property/:id',
    pathMatch: 'full',
  },
  {
    path: 'listing/:id',
    canActivate: [ListingCategoryGuard],
    component: NotFoundComponent, // Dummy-Komponente, wird nie angezeigt, da der Guard weiterleitet
  },
  // {
  //   path: 'login/:page',
  //   component: LoginComponent, // Dummy-Komponente, wird nie angezeigt, da der Guard weiterleitet
  // },
  {
    path: 'login/:page',
    component: LoginRegisterComponent, // Dummy-Komponente, wird nie angezeigt, da der Guard weiterleitet
  },
  {
    path: 'login',
    component: LoginRegisterComponent, // Dummy-Komponente, wird nie angezeigt, da der Guard weiterleitet
  },
  {
    path: 'notfound',
    component: NotFoundComponent,
  },
  // #########
  // User  Details
  {
    path: 'details-user/:id',
    loadComponent: () => import('./pages/details/details-user/details-user.component').then(m => m.DetailsUserComponent),
  },
  // #########
  // User  edit (lazy-loaded)
  {
    path: 'account',
    loadComponent: () => import('./pages/subscription/account/account.component').then(m => m.AccountComponent),
    canActivate: [AuthGuard],
  },
  {
    path: 'account/:id',
    loadComponent: () => import('./pages/subscription/account/account.component').then(m => m.AccountComponent),
    canActivate: [AuthGuard],
  },
  // #########
  // Create, Update Listings (lazy-loaded)
  {
    path: 'editBusinessListing/:id',
    loadComponent: () => import('./pages/subscription/edit-business-listing/edit-business-listing.component').then(m => m.EditBusinessListingComponent),
    canActivate: [AuthGuard],
  },
  {
    path: 'createBusinessListing',
    loadComponent: () => import('./pages/subscription/edit-business-listing/edit-business-listing.component').then(m => m.EditBusinessListingComponent),
    canActivate: [AuthGuard],
  },
  {
    path: 'editCommercialPropertyListing/:id',
    loadComponent: () => import('./pages/subscription/edit-commercial-property-listing/edit-commercial-property-listing.component').then(m => m.EditCommercialPropertyListingComponent),
    canActivate: [AuthGuard],
  },
  {
    path: 'createCommercialPropertyListing',
    loadComponent: () => import('./pages/subscription/edit-commercial-property-listing/edit-commercial-property-listing.component').then(m => m.EditCommercialPropertyListingComponent),
    canActivate: [AuthGuard],
  },
  // #########
  // My Listings (lazy-loaded)
  {
    path: 'myListings',
    loadComponent: () => import('./pages/subscription/my-listing/my-listing.component').then(m => m.MyListingComponent),
    canActivate: [AuthGuard],
  },
  // #########
  // My Favorites (lazy-loaded)
  {
    path: 'myFavorites',
    loadComponent: () => import('./pages/subscription/favorites/favorites.component').then(m => m.FavoritesComponent),
    canActivate: [AuthGuard],
  },
  // #########
  // Email Us (lazy-loaded)
  {
    path: 'emailUs',
    loadComponent: () => import('./pages/subscription/email-us/email-us.component').then(m => m.EmailUsComponent),
    // canActivate: [AuthGuard],
  },
  // #########
  // Logout
  {
    path: 'logout',
    component: LogoutComponent,
    canActivate: [AuthGuard],
  },
  // #########
  // Email Verification
  {
    path: 'emailVerification',
    component: EmailVerificationComponent,
  },
  {
    path: 'email-authorized',
    component: EmailAuthorizedComponent,
  },
  {
    path: 'success',
    component: SuccessComponent,
  },
  // #########
  // Admin Pages (lazy-loaded)
  {
    path: 'admin/users',
    loadComponent: () => import('./pages/admin/user-list/user-list.component').then(m => m.UserListComponent),
    canActivate: [AuthGuard],
  },
  // #########
  // Legal Pages
  {
    path: 'terms-of-use',
    component: TermsOfUseComponent,
  },
  {
    path: 'privacy-statement',
    component: PrivacyStatementComponent,
  },
  { path: '**', redirectTo: 'home' },
];
