import assert from 'assert';
import { db } from '../src/db/client';
import fs from 'fs';
import path from 'path';

async function runBrandingVerification() {
  console.log('\n======================================================');
  console.log('🎨 VERIFYING DYNAMIC BRANDING & LOGO SYSTEM');
  console.log('======================================================\n');

  let passed = 0;
  let total = 0;

  function test(name: string, fn: () => void | Promise<void>) {
    total++;
    try {
      const res = fn();
      if (res instanceof Promise) {
        return res
          .then(() => {
            console.log(`  ✓ ${name}`);
            passed++;
          })
          .catch((err) => {
            console.error(`  ✕ ${name}: ${err.message}`);
          });
      }
      console.log(`  ✓ ${name}`);
      passed++;
    } catch (err: any) {
      console.error(`  ✕ ${name}: ${err.message}`);
    }
  }

  // 1. Filesystem check
  test('Logo files exist in public/images, public/favicon, and public/uploads', () => {
    assert.strictEqual(fs.existsSync(path.resolve('public/images/logo.png')), true, 'logo.png must exist');
    assert.strictEqual(fs.existsSync(path.resolve('public/favicon.ico')), true, 'favicon.ico must exist');
    assert.strictEqual(fs.existsSync(path.resolve('public/uploads/branding/logo.png')), true, 'branding upload logo must exist');
  });

  // 2. MySQL Persistence
  await test('getSystemBranding returns accurate branding configuration', async () => {
    const b = await db.getSystemBranding();
    assert.ok(b, 'Branding record must exist');
    assert.ok(b.app_name, 'App name must be set');
    assert.ok(b.favicon_url, 'Favicon url must be present');
  });

  await test('updateSystemBranding updates in database and retrieves safely', async () => {
    const updated = await db.updateSystemBranding(
      {
        app_name: 'DigiPixora — Meta Pixel & Ads',
        logo_url: '/images/logo.png',
        favicon_url: '/favicon.ico',
      },
      'test_suite'
    );
    assert.strictEqual(updated.app_name, 'DigiPixora — Meta Pixel & Ads');
    assert.strictEqual(updated.logo_url, '/images/logo.png');

    const retrieved = await db.getSystemBranding();
    assert.strictEqual(retrieved.app_name, 'DigiPixora — Meta Pixel & Ads');
    assert.strictEqual(retrieved.logo_url, '/images/logo.png');
  });

  await test('resetSystemBranding restores defaults with correct logo', async () => {
    const reset = await db.resetSystemBranding('test_suite');
    assert.strictEqual(reset.app_name, 'DigiPixora — Meta Pixel & Ads');
    assert.strictEqual(reset.logo_url, '/images/logo.png');
    assert.strictEqual(reset.favicon_url, '/favicon.ico');
  });

  console.log('\n======================================================');
  console.log(`🎉 BRANDING SUITE COMPLETED: ${passed}/${total} PASSED, ${total - passed} FAILED`);
  console.log('======================================================\n');

  if (passed !== total) {
    process.exit(1);
  }
}

runBrandingVerification().catch((err) => {
  console.error('Test suite error:', err);
  process.exit(1);
});
