import { copyFile, mkdir, writeFile } from "node:fs/promises";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(__dirname, "..");

const defaultWebAppRoot = resolve(
  repoRoot,
  "..",
  "Company Documents",
  "Estate Security Platform Strategy",
  "FE",
  "WebApp"
);

const webAppRoot = resolve(process.env.GATEWISE_WEBAPP_ROOT || defaultWebAppRoot);
const sourceDir = join(repoRoot, "docs", "legal");
const targetDir = join(webAppRoot, "legal");

const policyDocs = [
  {
    slug: "terms-and-conditions",
    title: "Terms and Conditions",
    file: "terms-and-conditions.md",
    url: "https://gatewise.ng/legal/terms-and-conditions"
  },
  {
    slug: "privacy-policy",
    title: "Privacy Policy",
    file: "privacy-policy.md",
    url: "https://gatewise.ng/legal/privacy-policy"
  },
  {
    slug: "data-protection-policy",
    title: "Data Protection Policy",
    file: "data-protection-policy.md",
    url: "https://gatewise.ng/legal/data-protection-policy"
  },
  {
    slug: "data-retention-and-deletion-policy",
    title: "Data Retention and Deletion Policy",
    file: "data-retention-and-deletion-policy.md",
    url: "https://gatewise.ng/legal/data-retention-and-deletion-policy"
  },
  {
    slug: "cookies-and-cache-policy",
    title: "Cookies and Cache Policy",
    file: "cookies-and-cache-policy.md",
    url: "https://gatewise.ng/legal/cookies-and-cache-policy"
  },
  {
    slug: "data-processing-addendum",
    title: "Data Processing Addendum",
    file: "data-processing-addendum.md",
    url: "https://gatewise.ng/legal/data-processing-addendum"
  },
  {
    slug: "security-and-incident-response-policy",
    title: "Security and Incident Response Policy",
    file: "security-and-incident-response-policy.md",
    url: "https://gatewise.ng/legal/security-and-incident-response-policy"
  }
];

await mkdir(targetDir, { recursive: true });

for (const policy of policyDocs) {
  await copyFile(join(sourceDir, policy.file), join(targetDir, policy.file));
}

await writeFile(
  join(targetDir, "policy-links.json"),
  `${JSON.stringify(
    {
      product: "GateWise NG",
      company: "Globulus Technology Limited",
      policyCenter: "https://gatewise.ng/legal",
      policies: policyDocs.map(({ slug, title, url }) => ({ slug, title, url }))
    },
    null,
    2
  )}\n`,
  "utf8"
);

console.log(`Synced ${policyDocs.length} legal policy documents to ${targetDir}`);
