import { UserSession, UserRole, ScopeType } from '@/types';
import { GEOPOLITICAL_ZONES, NIGERIAN_STATES } from './electoralData';
import {
  SESSION_COOKIE_NAME,
  hashPin,
  createSessionToken,
  parseSessionToken
} from './session';

export { SESSION_COOKIE_NAME, hashPin, createSessionToken, parseSessionToken };

export function verifyPin(pin: string, storedHash: string, salt: string = 'adc-2027-presidential-salt-v2'): boolean {
  return hashPin(pin, salt) === storedHash;
}

// PII & Phone Number Privacy Masking
export function maskPhoneNumber(phone: string): string {
  if (!phone || phone.length < 7) return '0803***0000';
  return phone.slice(0, 4) + '***' + phone.slice(-4);
}

export function sanitizeUserForClient(user: UserSession): UserSession {
  return {
    ...user,
    phone_number: maskPhoneNumber(user.phone_number),
  };
}

// Pre-seeded Demo Accounts for instant role validation and testing
export const DEMO_USERS: Record<string, { user: UserSession; pinHash: string }> = {
  '25-11-01-001': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000001',
      full_name: 'Ibrahim Danjuma (ADC PU Agent)',
      phone_number: '08031112222',
      role: 'PU_AGENT',
      assigned_scope_type: 'PU',
      assigned_scope_id: '25-11-01-001',
      auth_identifier: '25-11-01-001',
    },
    pinHash: hashPin('1234'),
  },
  '24-11-01-001': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000001',
      full_name: 'Ibrahim Danjuma (ADC PU Agent)',
      phone_number: '08031112222',
      role: 'PU_AGENT',
      assigned_scope_type: 'PU',
      assigned_scope_id: '25-11-01-001',
      auth_identifier: '25-11-01-001',
    },
    pinHash: hashPin('1234'),
  },
  'WARD-25-11-01': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000002',
      full_name: 'Amina Bello (ADC Ward Supervisor)',
      phone_number: '08032223333',
      role: 'WARD_SUPERVISOR',
      assigned_scope_type: 'WARD',
      assigned_scope_id: '25-11-01',
      auth_identifier: 'WARD-25-11-01',
    },
    pinHash: hashPin('1234'),
  },
  'WARD-24-11-01': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000002',
      full_name: 'Amina Bello (ADC Ward Supervisor)',
      phone_number: '08032223333',
      role: 'WARD_SUPERVISOR',
      assigned_scope_type: 'WARD',
      assigned_scope_id: '25-11-01',
      auth_identifier: 'WARD-25-11-01',
    },
    pinHash: hashPin('1234'),
  },
  'LGA-25-11': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000003',
      full_name: 'Engr. Babajide Adeleke (ADC LGA Supervisor)',
      phone_number: '08033334444',
      role: 'LGA_SUPERVISOR',
      assigned_scope_type: 'LGA',
      assigned_scope_id: '25-11',
      auth_identifier: 'LGA-25-11',
    },
    pinHash: hashPin('1234'),
  },
  'LGA-24-11': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000003',
      full_name: 'Engr. Babajide Adeleke (ADC LGA Supervisor)',
      phone_number: '08033334444',
      role: 'LGA_SUPERVISOR',
      assigned_scope_type: 'LGA',
      assigned_scope_id: '25-11',
      auth_identifier: 'LGA-25-11',
    },
    pinHash: hashPin('1234'),
  },
  'STATE-25': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000004',
      full_name: 'Chief (Dr.) Olumide Peters (ADC Lagos State Supervisor)',
      phone_number: '08034445555',
      role: 'STATE_SUPERVISOR',
      assigned_scope_type: 'STATE',
      assigned_scope_id: '25',
      auth_identifier: 'STATE-25',
    },
    pinHash: hashPin('1234'),
  },
  'STATE-24': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000004',
      full_name: 'Chief (Dr.) Olumide Peters (ADC Lagos State Supervisor)',
      phone_number: '08034445555',
      role: 'STATE_SUPERVISOR',
      assigned_scope_type: 'STATE',
      assigned_scope_id: '25',
      auth_identifier: 'STATE-25',
    },
    pinHash: hashPin('1234'),
  },
  'STATE-22': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000021',
      full_name: 'Malam Bello Dantsoho (ADC Kebbi State Supervisor)',
      phone_number: '08034442121',
      role: 'STATE_SUPERVISOR',
      assigned_scope_type: 'STATE',
      assigned_scope_id: '22',
      auth_identifier: 'STATE-22',
    },
    pinHash: hashPin('1234'),
  },
  'STATE-21': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000021',
      full_name: 'Malam Bello Dantsoho (ADC Kebbi State Supervisor)',
      phone_number: '08034442121',
      role: 'STATE_SUPERVISOR',
      assigned_scope_type: 'STATE',
      assigned_scope_id: '22',
      auth_identifier: 'STATE-22',
    },
    pinHash: hashPin('1234'),
  },
  'REGION-SW': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000005',
      full_name: 'Otunba Kayode Balogun (ADC South West Regional Coordinator)',
      phone_number: '08035557777',
      role: 'REGIONAL_COORDINATOR',
      assigned_scope_type: 'REGION',
      assigned_scope_id: 'SOUTH_WEST',
      auth_identifier: 'REGION-SW',
    },
    pinHash: hashPin('1234'),
  },
  'REGION-NORTH_WEST': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000055',
      full_name: 'Dr. Usman Bello (ADC North West Regional Coordinator)',
      phone_number: '08035558888',
      role: 'REGIONAL_COORDINATOR',
      assigned_scope_type: 'REGION',
      assigned_scope_id: 'NORTH_WEST',
      auth_identifier: 'REGION-NORTH_WEST',
    },
    pinHash: hashPin('1234'),
  },
  'STATE-19': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000019',
      full_name: 'Alhaji Shehu Garba (ADC Kaduna State Supervisor)',
      phone_number: '08034441919',
      role: 'STATE_SUPERVISOR',
      assigned_scope_type: 'STATE',
      assigned_scope_id: '19',
      auth_identifier: 'STATE-19',
    },
    pinHash: hashPin('1234'),
  },
  'LGA-18-09': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000018',
      full_name: 'Comrade Sani Aliyu (ADC Kaduna North LGA Supervisor)',
      phone_number: '08033331818',
      role: 'LGA_SUPERVISOR',
      assigned_scope_type: 'LGA',
      assigned_scope_id: '25-11',
      auth_identifier: 'LGA-18-09',
    },
    pinHash: hashPin('1234'),
  },
  'WARD-18-09-10': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000010',
      full_name: 'Malama Hadiza Umar (ADC Kawo Ward Supervisor)',
      phone_number: '08032221810',
      role: 'WARD_SUPERVISOR',
      assigned_scope_type: 'WARD',
      assigned_scope_id: '25-11-01',
      auth_identifier: 'WARD-18-09-10',
    },
    pinHash: hashPin('1234'),
  },
  'NAT-DIR-001': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000006',
      full_name: 'Amb. Dr. Ralph Nwosu (ADC National Campaign Director)',
      phone_number: '08035556666',
      role: 'NATIONAL_COORDINATOR',
      assigned_scope_type: 'NATIONAL',
      assigned_scope_id: 'ALL',
      auth_identifier: 'NAT-DIR-001',
    },
    pinHash: hashPin('1234'),
  },
  'STATE-37': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000037',
      full_name: 'Hajiya Fatima Mohammed (ADC FCT Abuja Supervisor)',
      phone_number: '08034443737',
      role: 'STATE_SUPERVISOR',
      assigned_scope_type: 'STATE',
      assigned_scope_id: '37',
      auth_identifier: 'STATE-37',
    },
    pinHash: hashPin('1234'),
  },
  'STATE-38': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000037',
      full_name: 'Hajiya Fatima Mohammed (ADC FCT Abuja Supervisor)',
      phone_number: '08034443737',
      role: 'STATE_SUPERVISOR',
      assigned_scope_type: 'STATE',
      assigned_scope_id: '37',
      auth_identifier: 'STATE-37',
    },
    pinHash: hashPin('1234'),
  },
  'NAT-01': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000006',
      full_name: 'Amb. Dr. Ralph Nwosu (ADC National Campaign Director)',
      phone_number: '08035556666',
      role: 'NATIONAL_COORDINATOR',
      assigned_scope_type: 'NATIONAL',
      assigned_scope_id: 'ALL',
      auth_identifier: 'NAT-DIR-001',
    },
    pinHash: hashPin('1234'),
  },
  'GETOCORE-01': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000007',
      full_name: 'Engr. Chinedu Eze (GetoCore Technical Support Lead)',
      phone_number: '08036667777',
      role: 'GETOCORE_TECH_SUPPORT',
      assigned_scope_type: 'NATIONAL',
      assigned_scope_id: 'ALL',
      auth_identifier: 'GETOCORE-HQ-01',
    },
    pinHash: hashPin('1234'),
  },
  'GETOCORE-HQ-01': {
    user: {
      id: 'b0000000-0000-0000-0000-000000000007',
      full_name: 'Engr. Chinedu Eze (GetoCore Technical Support Lead)',
      phone_number: '08036667777',
      role: 'GETOCORE_TECH_SUPPORT',
      assigned_scope_type: 'NATIONAL',
      assigned_scope_id: 'ALL',
      auth_identifier: 'GETOCORE-HQ-01',
    },
    pinHash: hashPin('1234'),
  },
};



export function canAccessScope(
  userRole: UserRole,
  userScopeId: string,
  targetScopeType: ScopeType,
  targetScopeId: string
): boolean {
  if (userRole === 'NATIONAL_COORDINATOR' || userRole === 'GETOCORE_TECH_SUPPORT') return true;

  if (userRole === 'REGIONAL_COORDINATOR') {
    const zone = GEOPOLITICAL_ZONES.find((z) => z.code === userScopeId);
    if (!zone) return false;
    // Check if target state is in this region
    const targetState = NIGERIAN_STATES.find((s) => s.id === targetScopeId.slice(0, 2));
    if (targetState && targetState.geopolitical_zone === userScopeId) return true;
    return false;
  }

  if (userRole === 'STATE_SUPERVISOR') {
    return targetScopeId.startsWith(userScopeId);
  }
  if (userRole === 'LGA_SUPERVISOR') {
    return targetScopeId.startsWith(userScopeId);
  }
  if (userRole === 'WARD_SUPERVISOR') {
    return targetScopeId.startsWith(userScopeId);
  }
  if (userRole === 'PU_AGENT') {
    return userScopeId === targetScopeId;
  }
  return false;
}

