import { isNumber, isString } from "es-toolkit";

import type { ConfigValidationIssue } from "../../schema/issues";
import type { CheckerValidationInput } from "../types";

import { isPlainRecord, validateRawContentExpectations, validateRawValueExpectation } from "../../expect/validate";
import { issue, joinPath } from "../../schema/issues";

const VALID_ENCODINGS = new Set(["base64", "hex", "text"]);

export function validateUdpConfig(input: CheckerValidationInput): ConfigValidationIssue[] {
  const issues: ConfigValidationIssue[] = [];

  issues.push(...validateUdpDefaults(input));

  for (let i = 0; i < input.targets.length; i++) {
    const target = input.targets[i] as unknown;
    if (!isPlainRecord(target)) continue;
    if (target["type"] !== "udp") continue;
    issues.push(...validateUdpTarget(target, `targets[${i}]`));
  }

  return issues;
}

function getTargetName(target: Record<string, unknown>): string | undefined {
  if (isString(target["name"])) return target["name"];
  return isString(target["id"]) ? target["id"] : undefined;
}

function validateEncoding(value: unknown, path: string, targetName: string | undefined): ConfigValidationIssue[] {
  if (value === undefined) return [];
  if (!isString(value) || !VALID_ENCODINGS.has(value)) {
    return [issue("invalid-value", path, "必须为 text、hex 或 base64", targetName)];
  }
  return [];
}

function validateSize(value: unknown, path: string, targetName: string | undefined): ConfigValidationIssue[] {
  if (value === undefined) return [];
  if (!isString(value) && !(isNumber(value) && Number.isFinite(value) && value >= 0)) {
    return [issue("invalid-value", path, "必须为合法 size 值", targetName)];
  }
  return [];
}

function validateUdpDefaults(input: CheckerValidationInput): ConfigValidationIssue[] {
  const issues: ConfigValidationIssue[] = [];
  const defaults = input.defaults["udp"];
  if (defaults === undefined || defaults === null || !isPlainRecord(defaults)) return issues;

  const targetName = "defaults.udp";

  issues.push(...validateEncoding(defaults["encoding"], joinPath("defaults.udp", "encoding"), targetName));
  issues.push(
    ...validateEncoding(defaults["responseEncoding"], joinPath("defaults.udp", "responseEncoding"), targetName),
  );
  issues.push(...validateSize(defaults["maxResponseBytes"], joinPath("defaults.udp", "maxResponseBytes"), targetName));

  const allowedKeys = new Set(["encoding", "maxResponseBytes", "responseEncoding"]);
  for (const key of Object.keys(defaults)) {
    if (!allowedKeys.has(key)) {
      issues.push(issue("unknown-field", joinPath("defaults.udp", key), "是未知字段", targetName));
    }
  }

  return issues;
}

function validateUdpExpect(target: Record<string, unknown>, path: string): ConfigValidationIssue[] {
  const targetName = getTargetName(target);
  const expect = target["expect"];
  if (expect === undefined || expect === null || !isPlainRecord(expect)) return [];
  const issues: ConfigValidationIssue[] = [];
  const expectPath = joinPath(path, "expect");
  const responded: unknown = expect["responded"];

  if (responded !== undefined && typeof responded !== "boolean") {
    issues.push(issue("invalid-type", joinPath(expectPath, "responded"), "必须为布尔值", targetName));
  }

  if (expect["durationMs"] !== undefined) {
    issues.push(...validateRawValueExpectation(expect["durationMs"], joinPath(expectPath, "durationMs"), targetName));
  }

  if (expect["response"] !== undefined) {
    issues.push(...validateRawContentExpectations(expect["response"], joinPath(expectPath, "response"), targetName));
  }

  if (expect["responseSize"] !== undefined) {
    issues.push(
      ...validateRawValueExpectation(expect["responseSize"], joinPath(expectPath, "responseSize"), targetName),
    );
  }

  if (expect["sourceHost"] !== undefined) {
    issues.push(...validateRawValueExpectation(expect["sourceHost"], joinPath(expectPath, "sourceHost"), targetName));
  }

  if (expect["sourcePort"] !== undefined) {
    issues.push(...validateRawValueExpectation(expect["sourcePort"], joinPath(expectPath, "sourcePort"), targetName));
  }

  const respondedFalse = responded === false;
  if (respondedFalse) {
    if (expect["response"] !== undefined || expect["responseSize"] !== undefined) {
      issues.push(
        issue(
          "invalid-value",
          joinPath(expectPath, "responded"),
          "响应内容或大小断言需要 expect.responded 为 true",
          targetName,
        ),
      );
    }
    if (expect["sourceHost"] !== undefined || expect["sourcePort"] !== undefined) {
      issues.push(
        issue(
          "invalid-value",
          joinPath(expectPath, "responded"),
          "响应来源断言需要 expect.responded 为 true",
          targetName,
        ),
      );
    }
  }

  const allowedKeys = new Set(["durationMs", "responded", "response", "responseSize", "sourceHost", "sourcePort"]);
  for (const key of Object.keys(expect)) {
    if (!allowedKeys.has(key)) {
      issues.push(issue("unknown-field", joinPath(expectPath, key), "是未知字段", targetName));
    }
  }

  return issues;
}

function validateUdpTarget(target: Record<string, unknown>, path: string): ConfigValidationIssue[] {
  const issues: ConfigValidationIssue[] = [];
  const targetName = getTargetName(target);
  const udp = target["udp"];

  if (!isPlainRecord(udp)) {
    issues.push(issue("required", joinPath(path, "udp"), "缺少 udp 配置分组", targetName));
    issues.push(...validateUdpExpect(target, path));
    return issues;
  }

  if (!isString(udp["host"]) || udp["host"].trim() === "") {
    issues.push(issue("required", joinPath(joinPath(path, "udp"), "host"), "缺少 udp.host 字段", targetName));
  }

  if (udp["port"] === undefined) {
    issues.push(issue("required", joinPath(joinPath(path, "udp"), "port"), "缺少 udp.port 字段", targetName));
  } else if (!isNumber(udp["port"]) || !Number.isInteger(udp["port"]) || udp["port"] < 1 || udp["port"] > 65535) {
    issues.push(
      issue("invalid-value", joinPath(joinPath(path, "udp"), "port"), "必须为 1-65535 之间的整数", targetName),
    );
  }

  const encoding: unknown = udp["encoding"];
  issues.push(...validateEncoding(encoding, joinPath(joinPath(path, "udp"), "encoding"), targetName));

  if (encoding === "hex" && isString(udp["payload"])) {
    const hexPattern = /^[0-9a-fA-F]*$/;
    if (!hexPattern.test(udp["payload"])) {
      issues.push(
        issue(
          "invalid-value",
          joinPath(joinPath(path, "udp"), "payload"),
          "udp.payload 与 udp.encoding 不匹配",
          targetName,
        ),
      );
    }
  }

  if (encoding === "base64" && isString(udp["payload"])) {
    const base64Pattern = /^[A-Za-z0-9+/]*={0,2}$/;
    if (!base64Pattern.test(udp["payload"])) {
      issues.push(
        issue(
          "invalid-value",
          joinPath(joinPath(path, "udp"), "payload"),
          "udp.payload 与 udp.encoding 不匹配",
          targetName,
        ),
      );
    }
  }

  const responseEncoding: unknown = udp["responseEncoding"];
  issues.push(...validateEncoding(responseEncoding, joinPath(joinPath(path, "udp"), "responseEncoding"), targetName));

  issues.push(
    ...validateSize(udp["maxResponseBytes"], joinPath(joinPath(path, "udp"), "maxResponseBytes"), targetName),
  );

  const allowedUdpKeys = new Set(["encoding", "host", "maxResponseBytes", "payload", "port", "responseEncoding"]);
  for (const key of Object.keys(udp)) {
    if (!allowedUdpKeys.has(key)) {
      issues.push(issue("unknown-field", joinPath(joinPath(path, "udp"), key), "是未知字段", targetName));
    }
  }

  issues.push(...validateUdpExpect(target, path));

  return issues;
}
