const fs = require('fs');
const path = require('path');
const jwt = require('jsonwebtoken');
const dotenv = require('dotenv');

// Load environment variables from .env file
dotenv.config();

const generateAppleSecret = (config) => {
  const { teamId, clientId, keyId, privateKeyPath } = config;
  const privateKey = fs.readFileSync(privateKeyPath).toString();
  const now = Math.floor(Date.now() / 1000);
  const payload = {
    iss: teamId,
    iat: now,
    exp: now + 86400 * 180, // 180 days
    aud: 'https://appleid.apple.com',
    sub: clientId,
  };
  const headers = {
    alg: 'ES256',
    kid: keyId,
  };
  return jwt.sign(payload, privateKey, { algorithm: 'ES256', header: headers });
};

const config = {
  teamId: process.env.APPLE_TEAM_ID || '',
  clientId: process.env.AUTH_APPLE_ID || '',
  keyId: process.env.APPLE_KEY_ID || '',
  privateKeyPath: path.resolve(__dirname, process.env.APPLE_PRIVATE_KEY_PATH || ''),
};

if (!config.teamId || !config.clientId || !config.keyId || !config.privateKeyPath) {
  console.error('Missing necessary Apple configuration');
  process.exit(1);
}

const appleSecret = generateAppleSecret(config);
console.log(`Your Apple Secret:\n\n${appleSecret}\n`);
