/**
 * Script to create an API token for backend integration
 * Run this inside the CMS container if the UI doesn't work
 */

import { factories } from '@strapi/strapi';

async function createApiToken() {
  const strapi = await factories.createCoreStore()();
  
  try {
    // Create API token
    const tokenService = strapi.service('admin::api-token');
    
    const token = await tokenService.create({
      name: 'Backend Integration',
      description: 'Token for backend to fetch articles',
      type: 'read-only',
      permissions: [],
      lifespan: null, // Unlimited
    });

    console.log('✅ API Token created successfully!');
    console.log('\nToken details:');
    console.log('Name:', token.name);
    console.log('Type:', token.type);
    console.log('\nTOKEN (copy this to Coolify as STRAPI_API_TOKEN):');
    console.log(token.accessKey);
    console.log('\n⚠️  Save this token - you won\'t see it again!');
    
  } catch (error) {
    console.error('❌ Error creating API token:', error.message);
  } finally {
    await strapi.destroy();
  }
}

createApiToken();
