const APP_KEY = 'edr_passenger_app';

export type PassengerPermissionSeed = {
  id: string;
  key: string;
  name: { am: string; en: string };
  applicationKey: string;
};

const perm = (id: string, key: string, en: string): PassengerPermissionSeed => ({
  id,
  key,
  name: { am: en, en },
  applicationKey: APP_KEY,
});

export const PASSENGER_PERMISSIONS: PassengerPermissionSeed[] = [
  perm('40f1b49c-c33d-4563-a6bb-9373eabbde9b', 'edr_passenger_app:bookings:view',              'View bookings'),
  perm('62810ae5-315e-4ae5-8ed1-33cead51b95a', 'edr_passenger_app:bookings:manage',            'Manage bookings'),
  perm('b593adf3-2060-48b0-b35d-ff9ff5d72bc4', 'edr_passenger_app:bookings:cancel',            'Cancel bookings'),
  perm('566c968f-71f1-462d-9824-4b7cd33cecbb', 'edr_passenger_app:passengers:view',            'View passengers'),
  perm('ff5d33a0-0fe7-427f-a065-46dd14ac1da0', 'edr_passenger_app:passengers:manage',          'Manage passengers'),
  perm('326ec767-1da8-4c7e-b557-d4d2f9dd6d2c', 'edr_passenger_app:tickets:view',               'View tickets'),
  perm('8ec5697f-d2d4-40a2-a365-ad624991a2ab', 'edr_passenger_app:tickets:manage',             'Manage tickets'),
  perm('7f3a1e9c-2b4d-4c8a-9e6f-1a2b3c4d5e6f', 'edr_passenger_app:tickets:generate',           'Generate tickets'),
  perm('736aca18-6660-4865-9773-81a636f51fa0', 'edr_passenger_app:payments:view_all',          'View all payments'),
  perm('44065042-b4af-4af2-b213-34a823f78be1', 'edr_passenger_app:payments:refund',            'Refund payments'),
  perm('558f0172-ab9f-4d13-9477-4ca247d94f3c', 'edr_passenger_app:payments:manage_methods',   'Manage payment methods'),
  perm('bf6cbd48-7a4a-46ec-91f3-0a23245da0a4', 'edr_passenger_app:reports:view',              'View reports'),
  perm('53b49280-a272-4688-8345-e14fbedce50e', 'edr_passenger_app:fraud:view',                'View fraud alerts'),
  perm('834f576c-afe2-41f4-9e6e-5f87b155fbf4', 'edr_passenger_app:fraud:manage',              'Manage fraud rules'),
  perm('988b680d-df5a-4e79-9c01-c9441753ce1a', 'edr_passenger_app:audit:view',                'View audit logs'),
  perm('38736897-545f-47dc-9531-7fc381478a1f', 'edr_passenger_app:agents:view',               'View agents'),
  perm('30ada94a-cd15-4588-97bd-cd2f9d33ad7d', 'edr_passenger_app:agents:manage',             'Manage agents'),
  perm('75b5ff62-a8e4-4331-b6e6-d53e1456d10e', 'edr_passenger_app:currencies:manage',         'Manage currencies'),
  perm('4a47da9b-cf6e-4240-aff8-aadf01641c54', 'edr_passenger_app:notifications:send',        'Send notifications'),
  perm('bfe3428f-8b85-4a36-87c6-33063b084bf3', 'edr_passenger_app:dashboard:view',            'View dashboard'),

  // ── Master Data ────────────────────────────────────────────────────────────
  perm('102969bc-13a2-4f4a-aa7f-ce4b2599ce82', 'edr_passenger_app:stations:view',             'View stations'),
  perm('931cf6fc-8f41-46b6-82b8-b242f75d296e', 'edr_passenger_app:stations:manage',           'Manage stations'),
  perm('82a801b3-2451-409b-99e8-9f4d3515d329', 'edr_passenger_app:trains:view',               'View trains'),
  perm('e2a7f842-7691-4007-bd71-fd91c467044d', 'edr_passenger_app:trains:manage',             'Manage trains'),
  perm('71de593c-ae4a-4d15-9f0a-b889eeb4910c', 'edr_passenger_app:coaches:view',              'View coaches'),
  perm('39b3de2e-c779-4010-8ef3-d478f00a15c6', 'edr_passenger_app:coaches:manage',            'Manage coaches'),
  perm('ba0fdd0b-6580-48a8-b31f-eb5ef76a2453', 'edr_passenger_app:seats:view',                'View seats'),
  perm('6fb9affb-1885-446e-a8e4-962af9fae33f', 'edr_passenger_app:seats:manage',              'Manage seats'),
  perm('b7b659d9-f453-41d3-a6db-72e671446214', 'edr_passenger_app:classes:view',              'View classes'),
  perm('b2d06665-33f5-46d7-a895-785b5fb1896a', 'edr_passenger_app:classes:manage',            'Manage classes'),
  perm('8731ee98-24c2-4f8b-9c06-cf3fa900a95a', 'edr_passenger_app:routes:view',               'View routes'),
  perm('5851233c-78de-45b9-9d3f-63816d068622', 'edr_passenger_app:routes:manage',             'Manage routes'),
  perm('c453bdf9-496a-4ac8-b733-8eb5dd5d591a', 'edr_passenger_app:schedules:view',            'View schedules'),
  perm('d3f3cfd0-c7ce-47ab-be7f-bf3d6b40e488', 'edr_passenger_app:schedules:manage',          'Manage schedules'),

  // ── Tourism ────────────────────────────────────────────────────────────────
  perm('d78d810b-3003-4d81-92d5-41c437f3cc42', 'edr_passenger_app:packages:view',             'View packages'),
  perm('dcfab0d9-1f80-4822-892a-e2851b549297', 'edr_passenger_app:packages:manage',           'Manage packages'),
  perm('6d7ab68c-1b88-405d-9f92-b130055eece6', 'edr_passenger_app:inquiries:view',            'View package inquiries'),
  perm('dbe5a07a-d12f-4a36-b191-0bb4f980054e', 'edr_passenger_app:inquiries:manage',          'Manage package inquiries'),

  // ── Finance ────────────────────────────────────────────────────────────────
  perm('4b6efd87-f230-4109-abe1-593e53cb0c10', 'edr_passenger_app:tariff_rates:view',         'View tariff rates'),
  perm('94f17a59-397c-4c9c-a424-38a9c66c9e50', 'edr_passenger_app:tariff_rates:manage',       'Manage tariff rates'),
  perm('2dc4eb75-5b28-4ead-a2d4-82ac95cd290c', 'edr_passenger_app:payments:view',             'View payments'),
  perm('418b5f64-656b-4d44-a543-930ada9ec1a7', 'edr_passenger_app:payments:manage',           'Manage payments'),
  perm('3f3d5479-af33-4883-867e-aae9e2aeeeca', 'edr_passenger_app:currencies:view',           'View currencies'),
  perm('b4e63290-cc3a-4df8-9f2a-9ff726e86e36', 'edr_passenger_app:payment_methods:view',      'View payment methods'),
  perm('f9fb6af2-e869-4e6e-938c-259643393315', 'edr_passenger_app:payment_methods:manage',    'Manage payment methods'),

  perm('49fd28cd-5b58-4403-8e53-1df4b93cbbd2', 'edr_passenger_app:admin',                     'Full admin access'),
];

export const PASSENGER_PERMISSION_KEYS = PASSENGER_PERMISSIONS.map((p) => p.key);

export const PASSENGER_PERMS = {
  bookings: {
    view:   'edr_passenger_app:bookings:view',
    manage: 'edr_passenger_app:bookings:manage',
    cancel: 'edr_passenger_app:bookings:cancel',
  },
  passengers: {
    view:   'edr_passenger_app:passengers:view',
    manage: 'edr_passenger_app:passengers:manage',
  },
  tickets: {
    view:     'edr_passenger_app:tickets:view',
    manage:   'edr_passenger_app:tickets:manage',
    generate: 'edr_passenger_app:tickets:generate',
  },
  payments: {
    view:           'edr_passenger_app:payments:view',
    manage:         'edr_passenger_app:payments:manage',
    // legacy keys — retained as aliases for backward compatibility
    viewAll:        'edr_passenger_app:payments:view_all',
    refund:         'edr_passenger_app:payments:refund',
    manageMethods:  'edr_passenger_app:payments:manage_methods',
  },
  paymentMethods: {
    view:   'edr_passenger_app:payment_methods:view',
    manage: 'edr_passenger_app:payment_methods:manage',
  },
  stations: {
    view:   'edr_passenger_app:stations:view',
    manage: 'edr_passenger_app:stations:manage',
  },
  trains: {
    view:   'edr_passenger_app:trains:view',
    manage: 'edr_passenger_app:trains:manage',
  },
  coaches: {
    view:   'edr_passenger_app:coaches:view',
    manage: 'edr_passenger_app:coaches:manage',
  },
  seats: {
    view:   'edr_passenger_app:seats:view',
    manage: 'edr_passenger_app:seats:manage',
  },
  classes: {
    view:   'edr_passenger_app:classes:view',
    manage: 'edr_passenger_app:classes:manage',
  },
  routes: {
    view:   'edr_passenger_app:routes:view',
    manage: 'edr_passenger_app:routes:manage',
  },
  schedules: {
    view:   'edr_passenger_app:schedules:view',
    manage: 'edr_passenger_app:schedules:manage',
  },
  packages: {
    view:   'edr_passenger_app:packages:view',
    manage: 'edr_passenger_app:packages:manage',
  },
  inquiries: {
    view:   'edr_passenger_app:inquiries:view',
    manage: 'edr_passenger_app:inquiries:manage',
  },
  tariffRates: {
    view:   'edr_passenger_app:tariff_rates:view',
    manage: 'edr_passenger_app:tariff_rates:manage',
  },
  reports: {
    view: 'edr_passenger_app:reports:view',
  },
  fraud: {
    view:   'edr_passenger_app:fraud:view',
    manage: 'edr_passenger_app:fraud:manage',
  },
  audit: {
    view: 'edr_passenger_app:audit:view',
  },
  agents: {
    view:   'edr_passenger_app:agents:view',
    manage: 'edr_passenger_app:agents:manage',
  },
  currencies: {
    view:   'edr_passenger_app:currencies:view',
    manage: 'edr_passenger_app:currencies:manage',
  },
  notifications: {
    send: 'edr_passenger_app:notifications:send',
  },
  dashboard: {
    view: 'edr_passenger_app:dashboard:view',
  },
  admin: 'edr_passenger_app:admin',
} as const;

export const ROLE_PERMISSION_PRESETS = {
  backofficeAdmin: [...PASSENGER_PERMISSION_KEYS],

  stationMaster: [
    PASSENGER_PERMS.bookings.view,
    PASSENGER_PERMS.bookings.manage,
    PASSENGER_PERMS.tickets.view,
    PASSENGER_PERMS.tickets.manage,
    PASSENGER_PERMS.tickets.generate,
    PASSENGER_PERMS.passengers.view,
    PASSENGER_PERMS.agents.view,
    PASSENGER_PERMS.audit.view,
    PASSENGER_PERMS.notifications.send,
    PASSENGER_PERMS.dashboard.view,
  ],

  ticketOfficer: [
    PASSENGER_PERMS.tickets.view,
    PASSENGER_PERMS.tickets.manage,
    PASSENGER_PERMS.tickets.generate,
    PASSENGER_PERMS.bookings.view,
    PASSENGER_PERMS.passengers.view,
    PASSENGER_PERMS.dashboard.view,
  ],

  agent: [
    PASSENGER_PERMS.bookings.view,
    PASSENGER_PERMS.bookings.manage,
    PASSENGER_PERMS.bookings.cancel,
    PASSENGER_PERMS.passengers.view,
    PASSENGER_PERMS.tickets.view,
    PASSENGER_PERMS.tickets.manage,
    PASSENGER_PERMS.tickets.generate,
    PASSENGER_PERMS.payments.refund,
    PASSENGER_PERMS.dashboard.view,
  ],

  backofficeStaff: [
    PASSENGER_PERMS.bookings.view,
    PASSENGER_PERMS.passengers.view,
    PASSENGER_PERMS.tickets.view,
    PASSENGER_PERMS.dashboard.view,
  ],

  finance: [
    PASSENGER_PERMS.payments.view,
    PASSENGER_PERMS.payments.viewAll,
    PASSENGER_PERMS.payments.refund,
    PASSENGER_PERMS.reports.view,
    PASSENGER_PERMS.dashboard.view,
  ],

  operationsManager: [
    PASSENGER_PERMS.stations.view,   PASSENGER_PERMS.stations.manage,
    PASSENGER_PERMS.trains.view,     PASSENGER_PERMS.trains.manage,
    PASSENGER_PERMS.coaches.view,    PASSENGER_PERMS.coaches.manage,
    PASSENGER_PERMS.seats.view,      PASSENGER_PERMS.seats.manage,
    PASSENGER_PERMS.classes.view,    PASSENGER_PERMS.classes.manage,
    PASSENGER_PERMS.routes.view,     PASSENGER_PERMS.routes.manage,
    PASSENGER_PERMS.schedules.view,  PASSENGER_PERMS.schedules.manage,
    PASSENGER_PERMS.dashboard.view,
  ],

  marketingManager: [
    PASSENGER_PERMS.packages.view,   PASSENGER_PERMS.packages.manage,
    PASSENGER_PERMS.inquiries.view,  PASSENGER_PERMS.inquiries.manage,
    PASSENGER_PERMS.dashboard.view,
  ],

  financeManager: [
    PASSENGER_PERMS.tariffRates.view,      PASSENGER_PERMS.tariffRates.manage,
    PASSENGER_PERMS.payments.view,         PASSENGER_PERMS.payments.manage,
    PASSENGER_PERMS.currencies.view,       PASSENGER_PERMS.currencies.manage,
    PASSENGER_PERMS.paymentMethods.view,   PASSENGER_PERMS.paymentMethods.manage,
    PASSENGER_PERMS.reports.view,
    PASSENGER_PERMS.dashboard.view,
  ],
} as const;
