import { defineNuxtPlugin } from "#app";

/**
 * Halaman dibuka lewat `http://` di host produksi → paksa `https://` sebelum fetch login (POST).
 * Tanpa ini, `POST /workpulse-api/...` bisa kena 301 ke HTTPS dan body JSON hilang → login selalu gagal.
 */
export default defineNuxtPlugin({
  name: "workpulse-enforce-https",
  enforce: "pre",
  setup() {
    if (!import.meta.client) return;
    const { protocol, hostname, href } = window.location;
    if (protocol !== "http:") return;
    if (hostname === "localhost" || hostname === "127.0.0.1") return;
    window.location.replace(href.replace(/^http:/, "https:"));
  }
});
