import { Routes } from '@angular/router';
import { DashboardComponent } from './portals/user-portal/pages/dashboard/dashboard.component';
import { LoginPage } from './features/login-page/login-page';
import { UserPortalComponent } from './portals/user-portal/user-portal.component';
import { AuthGuard } from './core/guards/auth.guard';
import { PermissionGuard } from './core/guards/permission.guard';
import { PermissionModules } from './core/models/permission.model';
import { PermissionsPageComponent } from './features/permissions/pages/permissions-page/permissions-page.component';
import { MembersPageComponent } from './features/members/pages/members-page/members-page.component';
import { MinistriesPageComponent } from './features/ministry/pages/ministries-page/ministries-page.component';
import { UsersPageComponent } from './features/users/pages/users-page/users-page.component';
import { GivingCategoriesPageComponent } from './features/giving/pages/giving-categories-page/giving-categories-page.component';
import { GivingsPageComponent } from './features/giving/pages/givings-page/givings-page.component';
import { OfferingSessionPageComponent } from './features/giving/pages/offering-session-page/offering-session-page.component';
import { ExpenseCategoriesPageComponent } from './features/expense/pages/expense-categories-page/expense-categories-page.component';
import { ExpensesPageComponent } from './features/expense/pages/expenses-page/expenses-page.component';
import { ExpenseSnapshotsPageComponent } from './features/expense/pages/expense-snapshots-page/expense-snapshots-page.component';
import { MyReimbursementsPageComponent } from './features/expense/pages/my-reimbursements-page/my-reimbursements-page.component';
import { MonthlyStatementPageComponent } from './features/expense/pages/monthly-statement-page/monthly-statement-page.component';
import { FinanceDashboardPageComponent } from './features/finance-dashboard/pages/finance-dashboard-page/finance-dashboard-page.component';
import { DisbursementPageComponent } from './features/disbursement/pages/disbursement-page/disbursement-page.component';
import { CheckRegisterPageComponent } from './features/disbursement/pages/check-register-page/check-register-page.component';
import { ChurchProfilePageComponent } from './features/disbursement/pages/church-profile-page/church-profile-page.component';
import { Form990ReportPageComponent } from './features/finance-report/pages/form990-report-page/form990-report-page.component';
import { Form1099ReportPageComponent } from './features/finance-report/pages/form1099-report-page/form1099-report-page.component';
import { Payee1099PageComponent } from './features/payee1099/pages/payee-1099-page/payee-1099-page.component';
import { AttendanceCounterPageComponent } from './features/meal-attendance/pages/attendance-counter-page/attendance-counter-page.component';
import { OfferingEntryMobilePageComponent } from './features/giving/pages/offering-entry-mobile-page/offering-entry-mobile-page.component';
import { SystemLogsPageComponent } from './features/logging/pages/system-logs-page/system-logs-page.component';
import { AuditLogsPageComponent } from './features/logging/pages/audit-logs-page/audit-logs-page.component';
import { AccountSettingsPageComponent } from './features/account/pages/account-settings-page/account-settings-page.component';
import { AcceptInvitationComponent } from './features/accept-invitation/accept-invitation.component';

export const routes: Routes = [
    // Public routes
    { path: 'login', component: LoginPage },

    // Public first-login page — member sets their own password from a secret invitation link.
    { path: 'accept-invitation', component: AcceptInvitationComponent },

    // Public Sunday meal attendance counter — no login required (volunteers on phones).
    { path: 'attendance', component: AttendanceCounterPageComponent },

    // Public mobile Sunday offering entry — no login required (co-workers on phones).
    { path: 'offering-entry', component: OfferingEntryMobilePageComponent },

    // Keep the startup surface intentionally small: login + guarded mock dashboard.
    {
        path: 'user-portal',
        component: UserPortalComponent,
        canActivate: [AuthGuard],
        children: [
            { path: '',                  redirectTo: 'dashboard', pathMatch: 'full' },
            {
                path: 'dashboard',
                component: DashboardComponent,
                data: { title: 'Dashboard', titleZh: '首頁', section: 'Home' },
            },
            {
                path: 'account',
                component: AccountSettingsPageComponent,
                data: { title: 'Account Settings', titleZh: '帳戶設定', section: 'Account' },
            },
            {
                path: 'admin/members',
                component: MembersPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.Members, action: 'read' },
                    title: 'Member Management', titleZh: '會友管理', section: 'Admin',
                },
            },
            {
                path: 'admin/ministries',
                component: MinistriesPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.Ministries, action: 'read' },
                    title: 'Ministry Management', titleZh: '事工管理', section: 'Admin',
                },
            },
            {
                path: 'admin/users',
                component: UsersPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.Users, action: 'read' },
                    title: 'User Management', titleZh: '使用者管理', section: 'Admin',
                },
            },
            {
                path: 'admin/permissions',
                component: PermissionsPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.Permissions, action: 'read' },
                    title: 'Role Permissions', titleZh: '權限設定', section: 'Admin',
                },
            },
            {
                path: 'admin/logs/system',
                component: SystemLogsPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.SystemLogs, action: 'read' },
                    title: 'System Logs', titleZh: '系統日誌', section: 'Admin',
                },
            },
            {
                path: 'admin/logs/audit',
                component: AuditLogsPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.AuditLogs, action: 'read' },
                    title: 'Audit Logs', titleZh: '稽核日誌', section: 'Admin',
                },
            },
            {
                path: 'finance/dashboard',
                component: FinanceDashboardPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.FinanceDashboard, action: 'read' },
                    title: 'Finance Dashboard', titleZh: '財務儀表板', section: 'Finance',
                },
            },
            {
                path: 'finance/giving-categories',
                component: GivingCategoriesPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.GivingCategories, action: 'read' },
                    title: 'Giving Types', titleZh: '奉獻類型', section: 'Finance',
                },
            },
            {
                path: 'finance/givings',
                component: GivingsPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.Givings, action: 'read' },
                    title: 'Givings', titleZh: '單筆奉獻', section: 'Finance',
                },
            },
            {
                path: 'finance/offering-session',
                component: OfferingSessionPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.OfferingSessions, action: 'read' },
                    title: 'Sunday Offering Entry', titleZh: '主日奉獻錄入', section: 'Finance',
                },
            },
            {
                path: 'reimbursements',
                component: MyReimbursementsPageComponent,
                data: { title: 'My Reimbursements', titleZh: '我的報銷', section: 'Finance' },
            },
            {
                path: 'finance/expenses',
                component: ExpensesPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.Expenses, action: 'read' },
                    title: 'Expenses', titleZh: '支出', section: 'Finance',
                },
            },
            {
                path: 'finance/expense-snapshots',
                component: ExpenseSnapshotsPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    // Snapshots are a write-only management surface (the API gates every action on
                    // Expenses:Write), so require write — a read-only user has nothing to do here.
                    permission: { module: PermissionModules.Expenses, action: 'write' },
                    title: 'Expense Snapshots', titleZh: '費用範本', section: 'Finance',
                },
            },
            {
                path: 'finance/expense-categories',
                component: ExpenseCategoriesPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.ExpenseCategories, action: 'read' },
                    title: 'Expense Categories', titleZh: '費用類別', section: 'Finance',
                },
            },
            {
                path: 'finance/monthly-statement',
                component: MonthlyStatementPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.MonthlyStatements, action: 'read' },
                    title: 'Monthly Statement', titleZh: '月報表', section: 'Finance',
                },
            },
            {
                path: 'finance/disbursements',
                component: DisbursementPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.Disbursements, action: 'read' },
                    title: 'Disbursement Management', titleZh: '支票開立', section: 'Finance',
                },
            },
            {
                path: 'finance/check-register',
                component: CheckRegisterPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.Disbursements, action: 'read' },
                    title: 'Check Register', titleZh: '支票登記簿', section: 'Finance',
                },
            },
            {
                path: 'finance/church-profile',
                component: ChurchProfilePageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.ChurchProfile, action: 'read' },
                    title: 'Church Profile', titleZh: '教會資料', section: 'Finance',
                },
            },
            {
                path: 'finance/form-990-report',
                component: Form990ReportPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.Form990Report, action: 'read' },
                    title: 'Form 990 — Functional Expenses', titleZh: 'Form 990 功能性費用表', section: 'Finance',
                },
            },
            {
                path: 'finance/payee-1099',
                component: Payee1099PageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.Form1099, action: 'read' },
                    title: '1099 Recipients', titleZh: '1099 收款人', section: 'Finance',
                },
            },
            {
                path: 'finance/form1099-report',
                component: Form1099ReportPageComponent,
                canActivate: [PermissionGuard],
                data: {
                    permission: { module: PermissionModules.Form1099, action: 'read' },
                    title: '1099 Year-End Report', titleZh: '1099 年度報表', section: 'Finance',
                },
            },
        ]
    },

    { path: '', redirectTo: 'login', pathMatch: 'full' },
    { path: 'dashboard', redirectTo: 'user-portal/dashboard' },

    // Catch all route - redirect to login
    { path: '**', redirectTo: 'login' }
];
