import { HttpResponse, http } from "msw"

// --- Mock Data ---

export const mockMembersPage = {
  content: [
    {
      id: "m1",
      firstName: "Max",
      lastName: "Mustermann",
      email: "max@example.com",
      status: "ACTIVE",
      dateOfBirth: "1990-01-15",
    },
    {
      id: "m2",
      firstName: "Anna",
      lastName: "Schmidt",
      email: "anna@example.com",
      status: "ACTIVE",
      dateOfBirth: "1985-05-20",
    },
  ],
  totalElements: 2,
  totalPages: 1,
  number: 0,
  size: 20,
}

export const mockDistributionsPage = {
  content: [
    {
      id: "d1",
      memberId: "m1",
      memberName: "Max Mustermann",
      batchId: "b1",
      strainName: "Northern Lights",
      amountGrams: 5.0,
      distributedAt: "2025-06-10T14:30:00Z",
    },
  ],
  totalElements: 1,
  totalPages: 1,
  number: 0,
  size: 20,
}

export const mockBatchesPage = {
  content: [
    {
      id: "b1",
      strainName: "Northern Lights",
      thcPercent: 18.5,
      cbdPercent: 0.3,
      totalGrams: 500,
      remainingGrams: 200,
      status: "AVAILABLE",
      supplier: "Dutch Supply Co",
      harvestDate: "2025-05-01",
    },
  ],
  totalElements: 1,
  totalPages: 1,
  number: 0,
  size: 20,
}

export const mockClubStats = {
  totalMembers: 42,
  activeMembers: 38,
  totalDistributions: 156,
  totalGramsDistributed: 1240.5,
  activeBatches: 5,
  lowStockBatches: 1,
}

export const mockStaffList = [
  {
    id: "s1",
    email: "admin@club.de",
    displayName: "Admin User",
    role: "ADMIN",
    permissions: ["MANAGE_MEMBERS", "MANAGE_STOCK", "MANAGE_DISTRIBUTIONS"],
    status: "ACTIVE",
    lastLoginAt: "2025-06-10T10:00:00Z",
    createdAt: "2025-01-01T00:00:00Z",
  },
  {
    id: "s2",
    email: "staff@club.de",
    displayName: "Staff User",
    role: "STAFF",
    permissions: ["MANAGE_DISTRIBUTIONS"],
    status: "ACTIVE",
    lastLoginAt: "2025-06-09T08:00:00Z",
    createdAt: "2025-03-15T00:00:00Z",
  },
]

export const mockQuotaStatus = {
  memberId: "m1",
  monthlyLimitGrams: 50,
  usedGrams: 15,
  remainingGrams: 35,
  distributionCount: 3,
}

export const mockRecentDistributions = [
  {
    id: "d1",
    memberName: "Max Mustermann",
    strainName: "Northern Lights",
    amountGrams: 5.0,
    distributedAt: "2025-06-10T14:30:00Z",
  },
  {
    id: "d2",
    memberName: "Anna Schmidt",
    strainName: "Amnesia Haze",
    amountGrams: 3.0,
    distributedAt: "2025-06-09T11:00:00Z",
  },
]

// --- Handlers ---

export const handlers = [
  // Members
  http.get("/api/backend/members", () => {
    return HttpResponse.json(mockMembersPage)
  }),

  http.get("/api/backend/members/:id", ({ params }) => {
    const member = mockMembersPage.content.find((m) => m.id === params.id)
    if (!member) return new HttpResponse(null, { status: 404 })
    return HttpResponse.json(member)
  }),

  http.get("/api/backend/members/:id/quota", () => {
    return HttpResponse.json(mockQuotaStatus)
  }),

  http.post("/api/backend/members", async ({ request }) => {
    const body = (await request.json()) as Record<string, unknown>
    return HttpResponse.json({ id: "new-member-id", ...body, status: "ACTIVE" })
  }),

  http.put("/api/backend/members/:id", async ({ request, params }) => {
    const body = (await request.json()) as Record<string, unknown>
    return HttpResponse.json({ id: params.id, ...body })
  }),

  http.delete("/api/backend/members/:id", () => {
    return new HttpResponse(null, { status: 204 })
  }),

  // Distributions
  http.get("/api/backend/distributions", () => {
    return HttpResponse.json(mockDistributionsPage)
  }),

  http.get("/api/backend/distributions/recent", () => {
    return HttpResponse.json(mockRecentDistributions)
  }),

  http.post("/api/backend/distributions", async ({ request }) => {
    const body = (await request.json()) as Record<string, unknown>
    return HttpResponse.json({
      id: "new-dist-id",
      ...body,
      distributedAt: new Date().toISOString(),
    })
  }),

  // Stock / Batches
  http.get("/api/backend/batches", () => {
    return HttpResponse.json(mockBatchesPage)
  }),

  http.get("/api/backend/batches/available", () => {
    return HttpResponse.json(mockBatchesPage.content)
  }),

  http.get("/api/backend/batches/summary", () => {
    return HttpResponse.json([
      { strainName: "Northern Lights", totalGrams: 500, remainingGrams: 200 },
    ])
  }),

  http.post("/api/backend/batches", async ({ request }) => {
    const body = (await request.json()) as Record<string, unknown>
    return HttpResponse.json({
      id: "new-batch-id",
      ...body,
      status: "AVAILABLE",
      remainingGrams: body.totalGrams,
    })
  }),

  http.post("/api/backend/batches/:id/recall", ({ params }) => {
    return HttpResponse.json({
      id: params.id,
      status: "RECALLED",
    })
  }),

  // Dashboard
  http.get("/api/backend/dashboard/stats", () => {
    return HttpResponse.json(mockClubStats)
  }),

  // Staff
  http.get("/api/backend/staff", () => {
    return HttpResponse.json(mockStaffList)
  }),

  http.post("/api/backend/staff/invite", async ({ request }) => {
    const body = (await request.json()) as Record<string, unknown>
    return HttpResponse.json({
      id: "new-staff-id",
      ...body,
      status: "INVITED",
      createdAt: new Date().toISOString(),
    })
  }),

  http.put(
    "/api/backend/staff/:id/permissions",
    async ({ request, params }) => {
      const body = (await request.json()) as Record<string, unknown>
      return HttpResponse.json({
        id: params.id,
        ...body,
        status: "ACTIVE",
      })
    }
  ),

  http.post("/api/backend/staff/:id/revoke", ({ params: _params }) => {
    return new HttpResponse(null, { status: 204 })
  }),

  // Strains
  http.get("/api/backend/strains", () => {
    return HttpResponse.json([
      { id: "str1", name: "Northern Lights" },
      { id: "str2", name: "Amnesia Haze" },
    ])
  }),
]
