import { existsSync } from 'node:fs';
import AdmZip from 'adm-zip';
import { beforeAll, describe, expect, it } from 'vitest';
import { getCacheManager } from '../../../src/cache/cache-manager.js';
import { MojangDownloader } from '../../../src/downloaders/mojang-downloader.js';
import { verifyJavaVersion } from '../../../src/java/java-process.js';
import { getDecompileService } from '../../../src/services/decompile-service.js';
import { getMappingService } from '../../../src/services/mapping-service.js';
import { getRegistryService } from '../../../src/services/registry-service.js';
import { getRemapService } from '../../../src/services/remap-service.js';
import { TEST_MAPPING, TEST_VERSION } from './test-constants.js';

/**
 * Integration Test Suite for Minecraft 1.19.4
 *
 * Verifies older version support (1.19.x era)
 * Tests core functionality: download, remap (with direct JAR verification), decompile, registry extraction
 *
 * Run manually with: npm run test:manual:1.19.4
 */

describe(`Manual: Minecraft ${TEST_VERSION} Legacy Support`, () => {
  beforeAll(async () => {
    await verifyJavaVersion(17);
  }, 30000);

  describe('Core Pipeline', () => {
    it('should download client JAR', async () => {
      const downloader = new MojangDownloader();
      const jarPath = await downloader.downloadClientJar(TEST_VERSION);

      expect(jarPath).toBeDefined();
      expect(existsSync(jarPath)).toBe(true);
    }, 120000);

    it('should download Yarn mappings', async () => {
      const mappingService = getMappingService();
      const mappingPath = await mappingService.getMappings(TEST_VERSION, TEST_MAPPING);

      expect(mappingPath).toBeDefined();
      expect(existsSync(mappingPath)).toBe(true);
    }, 120000);

    it('should remap JAR with Yarn mappings', async () => {
      const remapService = getRemapService();
      const remappedPath = await remapService.getRemappedJar(TEST_VERSION, TEST_MAPPING);

      expect(remappedPath).toBeDefined();
      expect(existsSync(remappedPath)).toBe(true);
      expect(remappedPath).toContain('yarn');
    }, 300000);

    it('should contain human-readable class names in remapped JAR', async () => {
      const cacheManager = getCacheManager();
      const remappedJarPath = cacheManager.getRemappedJarPath(TEST_VERSION, TEST_MAPPING);

      expect(existsSync(remappedJarPath)).toBe(true);

      // Open the remapped JAR and verify class structure
      const zip = new AdmZip(remappedJarPath);
      const entries = zip.getEntries();

      // Find Entity class - should be in net/minecraft/entity/Entity.class (Yarn naming)
      const entityClass = entries.find((e) => e.entryName === 'net/minecraft/entity/Entity.class');
      expect(entityClass).toBeDefined();

      // Find Item class
      const itemClass = entries.find((e) => e.entryName === 'net/minecraft/item/Item.class');
      expect(itemClass).toBeDefined();

      // Find MinecraftServer class
      const serverClass = entries.find(
        (e) => e.entryName === 'net/minecraft/server/MinecraftServer.class',
      );
      expect(serverClass).toBeDefined();
    }, 30000);

    it('should have correct net.minecraft package structure', async () => {
      const cacheManager = getCacheManager();
      const remappedJarPath = cacheManager.getRemappedJarPath(TEST_VERSION, TEST_MAPPING);

      const zip = new AdmZip(remappedJarPath);
      const entries = zip.getEntries();

      // Count classes in net/minecraft/*
      const minecraftClasses = entries.filter(
        (e) => e.entryName.startsWith('net/minecraft/') && e.entryName.endsWith('.class'),
      );

      // Minecraft has thousands of classes
      expect(minecraftClasses.length).toBeGreaterThan(1000);

      // Verify known packages exist
      const hasEntityPackage = minecraftClasses.some((e) =>
        e.entryName.startsWith('net/minecraft/entity/'),
      );
      const hasItemPackage = minecraftClasses.some((e) =>
        e.entryName.startsWith('net/minecraft/item/'),
      );
      const hasBlockPackage = minecraftClasses.some((e) =>
        e.entryName.startsWith('net/minecraft/block/'),
      );

      expect(hasEntityPackage).toBe(true);
      expect(hasItemPackage).toBe(true);
      expect(hasBlockPackage).toBe(true);
    }, 30000);

    it('should not have single-letter obfuscated packages', async () => {
      const cacheManager = getCacheManager();
      const remappedJarPath = cacheManager.getRemappedJarPath(TEST_VERSION, TEST_MAPPING);

      const zip = new AdmZip(remappedJarPath);
      const entries = zip.getEntries();

      // Get all .class files
      const classFiles = entries.filter((e) => e.entryName.endsWith('.class'));

      // Check for obfuscated single-letter top-level packages (a/, b/, c/, etc.)
      const obfuscatedClasses = classFiles.filter((e) => {
        const parts = e.entryName.split('/');
        return parts[0].length === 1 && /^[a-z]$/.test(parts[0]);
      });

      // Should have very few or no obfuscated classes remaining
      expect(obfuscatedClasses.length).toBeLessThan(10);
    }, 30000);

    it('should decompile and get Entity class', async () => {
      const decompileService = getDecompileService();
      const source = await decompileService.getClassSource(
        TEST_VERSION,
        'net.minecraft.entity.Entity',
        TEST_MAPPING,
      );

      expect(source).toBeDefined();
      expect(source).toContain('class Entity');
    }, 600000);

    it('should extract block registry', async () => {
      const registryService = getRegistryService();
      const blocks = await registryService.getRegistryData(TEST_VERSION, 'block');

      expect(blocks).toBeDefined();
      expect(blocks.entries).toBeDefined();
      expect(blocks.entries['minecraft:stone']).toBeDefined();
    }, 300000);
  });

  describe('Mapping Lookups', () => {
    /**
     * Tests mapping lookup functionality for this version.
     * Covers single-file lookups and two-step bridge lookups.
     */

    it('should lookup intermediary → yarn class mapping', async () => {
      const mappingService = getMappingService();

      // class_1297 is the intermediary name for Entity
      const result = await mappingService.lookupMapping(
        TEST_VERSION,
        'net/minecraft/class_1297',
        'intermediary',
        'yarn',
      );

      expect(result.found).toBe(true);
      expect(result.type).toBe('class');
      expect(result.target).toContain('Entity');
    }, 60000);

    it('should lookup yarn → intermediary class mapping', async () => {
      const mappingService = getMappingService();

      const result = await mappingService.lookupMapping(
        TEST_VERSION,
        'net/minecraft/entity/Entity',
        'yarn',
        'intermediary',
      );

      expect(result.found).toBe(true);
      expect(result.type).toBe('class');
      expect(result.target).toContain('class_');
    }, 60000);

    it('should lookup intermediary → mojmap class mapping', async () => {
      const mappingService = getMappingService();

      const result = await mappingService.lookupMapping(
        TEST_VERSION,
        'net/minecraft/class_1297',
        'intermediary',
        'mojmap',
      );

      expect(result.found).toBe(true);
      expect(result.type).toBe('class');
      expect(result.target).toContain('Entity');
    }, 60000);

    it('should lookup mojmap → intermediary class mapping', async () => {
      const mappingService = getMappingService();

      const result = await mappingService.lookupMapping(
        TEST_VERSION,
        'net/minecraft/world/entity/Entity',
        'mojmap',
        'intermediary',
      );

      expect(result.found).toBe(true);
      expect(result.type).toBe('class');
      expect(result.target).toContain('class_');
    }, 60000);

    it('should lookup intermediary → official class mapping', async () => {
      const mappingService = getMappingService();

      const result = await mappingService.lookupMapping(
        TEST_VERSION,
        'net/minecraft/class_1297',
        'intermediary',
        'official',
      );

      expect(result.found).toBe(true);
      expect(result.type).toBe('class');
      expect(result.target).toBeDefined();
      // Obfuscated names are typically short
      expect(result.target?.length).toBeLessThan(50);
    }, 60000);

    it('should lookup official → intermediary class mapping', async () => {
      const mappingService = getMappingService();

      // First get an obfuscated name
      const intResult = await mappingService.lookupMapping(
        TEST_VERSION,
        'net/minecraft/class_1297',
        'intermediary',
        'official',
      );

      expect(intResult.found).toBe(true);
      const obfuscatedName = intResult.target as string;

      // Now lookup back to intermediary
      const result = await mappingService.lookupMapping(
        TEST_VERSION,
        obfuscatedName,
        'official',
        'intermediary',
      );

      expect(result.found).toBe(true);
      expect(result.type).toBe('class');
      expect(result.target).toContain('class_1297');
    }, 60000);

    it('should lookup yarn → mojmap (two-step bridge)', async () => {
      const mappingService = getMappingService();

      const result = await mappingService.lookupMapping(
        TEST_VERSION,
        'net/minecraft/entity/Entity',
        'yarn',
        'mojmap',
      );

      expect(result.found).toBe(true);
      expect(result.type).toBe('class');
      // Mojmap uses net/minecraft/world/entity/Entity
      expect(result.target).toContain('Entity');
    }, 120000);

    it('should lookup mojmap → yarn (two-step bridge)', async () => {
      const mappingService = getMappingService();

      const result = await mappingService.lookupMapping(
        TEST_VERSION,
        'net/minecraft/world/entity/Entity',
        'mojmap',
        'yarn',
      );

      expect(result.found).toBe(true);
      expect(result.type).toBe('class');
      expect(result.target).toContain('Entity');
    }, 120000);
  });
});
