import { execSync } from "child_process";
import fs from "fs";
import path from "path";

const generatedDir = "../native-helpers/windows-helper/src/Models/Generated";

try {
  // Remove existing generated models and create the directory
  if (fs.existsSync(generatedDir)) {
    fs.rmSync(generatedDir, { recursive: true, force: true });
  }
  fs.mkdirSync(generatedDir, { recursive: true });

  console.log("Directory created/cleaned successfully.");

  // Generate C# models from JSON schemas using quicktype
  // Using System.Text.Json mode with proper nullable reference type support
  // quicktype only supports csharp 5 and 6, so using 6
  const command =
    "quicktype --src-lang schema --lang csharp " +
    `--namespace WindowsHelper.Models ` +
    `--framework SystemTextJson ` +
    `--array-type list ` +
    `--csharp-version 6 ` +
    `--features complete ` +
    `--no-check-required ` +
    `-o ${generatedDir}/Models.cs ` +
    "generated/json-schemas/rpc/rpc-request.schema.json " +
    "generated/json-schemas/rpc/rpc-response.schema.json " +
    "generated/json-schemas/methods/get-accessibility-tree-details-params.schema.json " +
    "generated/json-schemas/methods/get-accessibility-tree-details-result.schema.json " +
    "generated/json-schemas/methods/get-accessibility-context-params.schema.json " +
    "generated/json-schemas/methods/get-accessibility-context-result.schema.json " +
    "generated/json-schemas/methods/paste-text-params.schema.json " +
    "generated/json-schemas/methods/paste-text-result.schema.json " +
    "generated/json-schemas/methods/mute-system-audio-params.schema.json " +
    "generated/json-schemas/methods/mute-system-audio-result.schema.json " +
    "generated/json-schemas/methods/restore-system-audio-params.schema.json " +
    "generated/json-schemas/methods/restore-system-audio-result.schema.json " +
    "generated/json-schemas/methods/set-shortcuts-params.schema.json " +
    "generated/json-schemas/methods/set-shortcuts-result.schema.json " +
    "generated/json-schemas/events/key-down-event.schema.json " +
    "generated/json-schemas/events/key-up-event.schema.json " +
    "generated/json-schemas/events/flags-changed-event.schema.json " +
    "generated/json-schemas/events/helper-event.schema.json";

  console.log(`Executing quicktype...`);
  execSync(command, { stdio: "inherit" });

  // Post-process to clean up the generated code
  let content = fs.readFileSync(path.join(generatedDir, "Models.cs"), "utf8");

  // Remove the "Schema" suffix from class names
  content = content.replace(/(\w+)Schema/g, "$1");

  // Fix the RPC types to use simpler names
  content = content.replace(
    /public partial class RpcRequest\b/g,
    "public partial class RpcRequest",
  );
  content = content.replace(
    /public partial class RpcResponse\b/g,
    "public partial class RpcResponse",
  );

  // Add a header comment
  const header = `// <auto-generated />
// This file was automatically generated by quicktype from JSON schemas.
// DO NOT EDIT THIS FILE DIRECTLY! Instead, edit the TypeScript schemas and regenerate.

`;

  content = header + content;

  fs.writeFileSync(path.join(generatedDir, "Models.cs"), content);

  console.log("C# models generated successfully.");
} catch (error) {
  console.error("Error generating C# models:", error);
  process.exit(1);
}
