import { http, HttpResponse } from "msw";

export const handlers = [
  // GET requests via proxy.php
  http.get("/proxy.php", ({ request }) => {
    const url = new URL(request.url);
    const path = url.searchParams.get("path");

    if (path === "/auth/me") {
      return HttpResponse.json({
        status: true,
        data: {
          user: {
            id: 1,
            gauth_user_id: "u_123",
            email: "test@example.com",
            display_name: "Test User",
            avatar_url: "",
            system_role: "admin",
            status: "active",
          },
          permissions: [
            "areas.view", "areas.manage",
            "devices.view", "devices.scan", "devices.setup", "devices.control", "devices.edit", "devices.delete",
            "scripts.view", "scripts.run", "scripts.edit",
            "firmware.view", "firmware.upload",
          ],
        },
      });
    }

    if (path === "/api/v1/areas/list") {
      return HttpResponse.json({
        status: true,
        data: {
          areas: [
            { id: 1, type: "room", alias: "kitchen", display_name: "Kitchen", parent_id: 0 },
            { id: 2, type: "room", alias: "hall", display_name: "Hall", parent_id: 0 },
          ],
          total: 2,
        },
      });
    }

    if (path === "/api/v1/devices/scanning/setup") {
      return HttpResponse.json({
        status: true,
        data: {
          devices: [
            {
              device_name: "New Device",
              device_type: "relay",
              ip_address: "192.168.1.50",
              mac_address: "A4:CF:12:9B:3F:D2",
              firmware_version: "1.0",
              status: "setup",
            },
          ],
        },
      });
    }

    if (path === "/api/v1/devices/scanning/all") {
      return HttpResponse.json({
        status: true,
        data: {
          devices: [
            {
              device_name: "Kitchen Relay",
              device_type: "relay",
              ip_address: "192.168.1.10",
              mac_address: "A4:CF:12:9B:3F:00",
              firmware_version: "1.2",
              status: "normal",
            },
            {
              device_name: "New Device",
              device_type: "relay",
              ip_address: "192.168.1.50",
              mac_address: "A4:CF:12:9B:3F:D2",
              firmware_version: "1.0",
              status: "setup",
            },
          ],
        },
      });
    }

    if (path === "/api/v1/devices/list") {
      return HttpResponse.json({
        status: true,
        data: {
          devices: [
            { id: 1, name: "Relay 1", alias: "relay_1", device_type: "relay", device_ip: "192.168.1.10", connection_status: "active", area_id: 1 },
          ],
          total: 1,
        },
      });
    }

    if (path?.startsWith("/api/v1/devices/id/") && !path.includes("/status") && !path.endsWith("/remove") && !path.endsWith("/unassign-from-area") && !path.endsWith("/reboot")) {
      return HttpResponse.json({
        status: true,
        data: {
          device: {
            id: 1,
            alias: "relay_1",
            name: "Relay 1",
            device_type: "relay",
            device_ip: "192.168.1.10",
            device_mac: "A4:CF:12:9B:3F:00",
            device_hard_id: "abc123",
            firmware_version: "1.2",
            connection_status: "active",
            status: "active",
            description: "Test relay device",
            last_contact: "2026-06-01 12:00:00",
            create_at: "2026-01-01 00:00:00",
            area_id: 1,
          },
        },
      });
    }

    if (path?.startsWith("/api/v1/devices/id/") && path.endsWith("/status")) {
      return HttpResponse.json({
        status: true,
        data: {
          device: {
            id: 1,
            alias: "relay_1",
            device_response: {
              status: "ok",
              channels: [
                { channel: 0, state: "on", type: "relay" },
                { channel: 1, state: "off", type: "relay" },
              ],
            },
          },
        },
      });
    }

    if (path?.startsWith("/api/v1/devices/id/") && path.endsWith("/reboot")) {
      return HttpResponse.json({ status: true });
    }

    if (path?.startsWith("/api/v1/devices/id/") && path.endsWith("/remove")) {
      return HttpResponse.json({ status: true });
    }

    if (path?.startsWith("/api/v1/devices/id/") && path.endsWith("/unassign-from-area")) {
      return HttpResponse.json({ status: true });
    }

    if (path === "/api/v1/scripts/actions/list") {
      return HttpResponse.json({
        status: true,
        data: {
          scripts: [
            { id: 1, alias: "kitchen_light", name: "Kitchen Light", icon: '<i class="ph ph-lightbulb"></i>', state: "enabled", author: "Test", scope: "KitchenScope", area_id: 1 },
            { id: 2, alias: "hall_light", name: "Hall Light", icon: '<i class="ph ph-lightbulb"></i>', state: "enabled", author: "Test", scope: "HallScope" },
          ],
          total: 2,
        },
      });
    }

    if (path === "/api/v1/scripts/regular/list") {
      return HttpResponse.json({
        status: true,
        data: {
          scripts: [
            { id: 3, alias: "auto_off", name: "Auto Off", state: "enabled", filename: "auto_off.php", scope: "KitchenScope", area_id: 1 },
          ],
          total: 1,
        },
      });
    }

    if (path === "/api/v1/scripts/scopes/list") {
      return HttpResponse.json({
        status: true,
        data: {
          scopes: [
            { name: "KitchenScope", filename: "KitchenScope.php", state: "enabled", path: "/srv/.../ControlScripts" },
            { name: "HallScope", filename: "HallScope.php", state: "enabled", path: "/srv/.../ControlScripts" },
          ],
          total: 2,
        },
      });
    }

    if (path?.startsWith("/api/v1/areas/id/") && path.endsWith("/remove")) {
      return HttpResponse.json({ status: true });
    }

    if (path?.startsWith("/api/v1/scripts/actions/alias/") && (path.endsWith("/enable") || path.endsWith("/disable"))) {
      return HttpResponse.json({ status: true });
    }

    if (path?.startsWith("/api/v1/scripts/scopes/name/") && (path.endsWith("/enable") || path.endsWith("/disable"))) {
      return HttpResponse.json({ status: true });
    }

    if (path?.startsWith("/api/v1/scripts/scopes/name/")) {
      return HttpResponse.text("<?php\n// Sample scope code\nclass KitchenScope {}");
    }

    if (path === "/api/v1/scripts/place-in-area") {
      return HttpResponse.json({ status: true });
    }

    if (path?.startsWith("/api/v1/scripts/id/") && path.endsWith("/unassign-from-area")) {
      return HttpResponse.json({ status: true });
    }

    if (path?.startsWith("/api/v1/areas/id/") && path.endsWith("/devices")) {
      return HttpResponse.json({
        status: true,
        data: {
          devices: [
            { id: 1, name: "Relay 1", alias: "relay_1", device_type: "relay", device_ip: "192.168.1.10", connection_status: "active" },
          ],
          total: 1,
        },
      });
    }

    if (path?.startsWith("/api/v1/areas/id/") && path.endsWith("/scripts")) {
      return HttpResponse.json({
        status: true,
        data: {
          scripts: [
            { alias: "kitchen_light", name: "Kitchen Light", state: "enabled", type: "action", area_id: 1, scope: "KitchenScope" },
            { alias: "auto_off", name: "Auto Off", state: "enabled", type: "regular", area_id: 1, scope: "KitchenScope", filename: "auto_off.php" },
          ],
          total: 2,
        },
      });
    }

    if (path?.startsWith("/api/v1/areas/id/") && path.endsWith("/unassign-from-area")) {
      return HttpResponse.json({ status: true });
    }

    return new HttpResponse(null, { status: 404 });
  }),

  // POST requests via proxy.php
  http.post("/proxy.php", async ({ request }) => {
    const url = new URL(request.url);
    const path = url.searchParams.get("path");
    const body = await request.json().catch(() => ({}));

    if (path === "/auth/logout") {
      return HttpResponse.json({ status: true });
    }

    if (path === "/auth/refresh") {
      return HttpResponse.json({
        status: true,
        data: {
          access_token: "mock_access_token_12345",
          expires_in: 900,
        },
      });
    }

    if (path === "/api/v1/areas/new-area") {
      return HttpResponse.json({
        status: true,
        data: {
          area: { id: 3, type: body.type, alias: body.alias, display_name: body.display_name, parent_id: 0 },
        },
      });
    }

    if (path === "/api/v1/areas/update-display-name") {
      return HttpResponse.json({
        status: true,
        data: { area_id: body.area_id, display_name: body.display_name },
      });
    }

    if (path === "/api/v1/devices/setup/new-device") {
      return HttpResponse.json({
        status: true,
        data: {
          device: { id: 2, name: body.name, alias: body.alias, device_type: "relay", device_ip: body.device_ip },
        },
      });
    }

    if (path === "/api/v1/devices/place-in-area") {
      return HttpResponse.json({ status: true });
    }

    if (path === "/api/v1/scripts/actions/run") {
      return HttpResponse.json({
        status: true,
        data: {
          return: {
            result: { ok: true },
            exec_time: "0.042 seconds",
          },
        },
      });
    }

    if (path === "/api/v1/devices/update-name") {
      return HttpResponse.json({ status: true });
    }

    if (path === "/api/v1/devices/update-description") {
      return HttpResponse.json({ status: true });
    }

    if (path === "/api/v1/devices/update-alias") {
      return HttpResponse.json({ status: true });
    }

    return new HttpResponse(null, { status: 404 });
  }),
];
