/* Seed contact-field permissions.
 * - Generates permissions like:
 *   name: contact.name, resource: contact, action: view_name
 * - Uses per-field action (view_<field>) so it works even if DB has a UNIQUE(resource, action) index.
 * - Upserts by unique key (typically name) without failing if it already exists.
 */
const { pool } = require('../config/database');
const rawFields = [
    'name',
    'organization',
    'rank',
    'service',
    'login',
    'unit',
    'department',
    'designation',
    'phone',
    'alternative_mobile',
    'present_address',
    'present_lat_lng',
    'permanent_address',
    'permanent_lat_lng',
    'office_address',
    'office_lat_lng',
    'email',
    'occupation',
    'organization', // duplicate
    'designation', // duplicate
    'facebook',
    'linkedin',
    'designation_name'
];
const uniqueFields = Array.from(new Set(rawFields.map(f => String(f).trim()).filter(Boolean)));
async function seed() {
    console.log('🔑 Seeding contact field permissions...');
    const insertSql = `
    INSERT INTO permissions (name, description, resource, action, is_active, created_at, updated_at)
    VALUES (?, ?, ?, ?, 1, NOW(), NOW())
    ON DUPLICATE KEY UPDATE
      description = VALUES(description),
      resource = VALUES(resource),
      action = VALUES(action),
      is_active = 1,
      updated_at = NOW()
  `;
    let created = 0;
    for (const field of uniqueFields) {
        const safeField = field.toLowerCase().replace(/[^a-z0-9_]+/g, '_').replace(/_+/g, '_').replace(/^_+|_+$/g, '');
        const name = `contact.${safeField}`;
        const resource = 'contact';
        const action = `view_${safeField}`; // per-field action to avoid (resource, action) uniqueness conflicts
        const description = `Permission to view contact field "${field}"`;
        try {
            await pool.execute(insertSql, [name, description, resource, action]);
            created++;
            console.log(`✅ Upserted: ${name} (${action})`);
        }
        catch (err) {
            console.error(`❌ Failed for ${name}:`, err.message);
        }
    }
    console.log(`🎉 Done. Upserted ${created} permissions.`);
}
seed()
    .then(() => process.exit(0))
    .catch(err => {
    console.error('❌ Seed error:', err);
    process.exit(1);
});
//# sourceMappingURL=seed_contact_field_permissions.js.map