import Orchestrator from '../../orchestrator';
import UnityVersioning from '../../../unity-versioning';
import { Cli } from '../../../cli/cli';
import OrchestratorLogger from '../../services/core/orchestrator-logger';
import { v4 as uuidv4 } from 'uuid';
import OrchestratorOptions from '../../options/orchestrator-options';
import setups from '../orchestrator-suite.test';
import BuildParameters from '../../../build-parameters';
import ImageTag from '../../../image-tag';

async function CreateParameters(overrides: any) {
  if (overrides) {
    Cli.options = overrides;
  }

  return await BuildParameters.create();
}

describe('Orchestrator Kubernetes', () => {
  it('Responds', () => {});
  setups();

  if (OrchestratorOptions.orchestratorDebug) {
    const enableK8sE2E = process.env.ENABLE_K8S_E2E === 'true';

    const testBody = async () => {
      if (OrchestratorOptions.providerStrategy !== `k8s`) {
        return;
      }
      process.env.USE_IL2CPP = 'false';
      const overrides = {
        versioning: 'None',
        projectPath: 'test-project',
        unityVersion: UnityVersioning.determineUnityVersion('test-project', UnityVersioning.read('test-project')),
        targetPlatform: 'StandaloneLinux64',
        cacheKey: `test-case-${uuidv4()}`,
        providerStrategy: 'k8s',
        buildPlatform: 'linux',
        orchestratorDebug: true,
      };
      const buildParameter = await CreateParameters(overrides);
      expect(buildParameter.projectPath).toEqual(overrides.projectPath);

      const baseImage = new ImageTag(buildParameter);
      const resultsObject = await Orchestrator.run(buildParameter, baseImage.toString());
      const results = resultsObject.BuildResults;
      const libraryString = 'Rebuilding Library because the asset database could not be found!';
      const cachePushFail = 'Did not push source folder to cache because it was empty Library';
      const buildSucceededString = 'Build succeeded';

      const fallbackLogsUnavailableMessage =
        'Pod logs unavailable - pod may have been terminated before logs could be collected.';
      const incompleteLogsMessage =
        'Pod logs incomplete - "Collected Logs" marker not found. Pod may have been terminated before post-build completed.';

      // Check if pod was evicted due to resource constraints - this is a test infrastructure failure
      // Evictions indicate the cluster doesn't have enough resources, which is a test environment issue
      if (
        results.includes('The node was low on resource: ephemeral-storage') ||
        results.includes('TerminationByKubelet') ||
        results.includes('Evicted')
      ) {
        throw new Error(
          `Test failed: Pod was evicted due to resource constraints (ephemeral-storage). ` +
            `This indicates the test environment doesn't have enough disk space. ` +
            `Results: ${results.slice(0, 500)}`,
        );
      }

      // If we hit the aggressive fallback path and couldn't retrieve any logs from the pod,
      // don't assert on specific Unity log contents – just assert that we got the fallback message.
      // This makes the test resilient to cluster-level evictions / PreStop hook failures while still
      // ensuring Orchestrator surfaces a useful message in BuildResults.
      // However, if we got logs but they're incomplete (missing "Collected Logs"), the test should fail
      // as this indicates the build didn't complete successfully (pod was evicted/killed).
      if (results.includes(fallbackLogsUnavailableMessage)) {
        // Complete failure - no logs at all (acceptable for eviction scenarios)
        expect(results).toContain(fallbackLogsUnavailableMessage);
        OrchestratorLogger.log('Test passed with fallback message (pod was evicted before any logs were written)');
      } else if (results.includes(incompleteLogsMessage)) {
        // Incomplete logs - we got some output but missing "Collected Logs" (build didn't complete)
        // This should fail the test as the build didn't succeed
        throw new Error(
          `Build did not complete successfully: ${incompleteLogsMessage}\n` +
            `This indicates the pod was evicted or killed before post-build completed.\n` +
            `Build results:\n${results.slice(0, 500)}`,
        );
      } else {
        // Normal case - logs are complete
        expect(results).toContain('Collected Logs');
        expect(results).toContain(libraryString);
        expect(results).toContain(buildSucceededString);
        expect(results).not.toContain(cachePushFail);
      }

      OrchestratorLogger.log(`run 1 succeeded`);
    };

    if (enableK8sE2E) {
      it('Run one build it using K8s without error', testBody, 1_000_000_000);
    } else {
      it.skip('Run one build it using K8s without error - disabled (no outbound network)', () => {
        OrchestratorLogger.log('Skipping K8s e2e (ENABLE_K8S_E2E not true)');
      });
    }
  }
});
