import { NextResponse, NextRequest } from "next/server";

interface APiData {
  uuid: string;
  firstName: string;
  lastName: string;
  email: string;
  phoneNumber: string;
  country: string;
  description: string;
  isDeleted: boolean;
  isConfirmed: boolean;
  createdAt: Date;
  updatedAt: Date;
}

function generateMockData(volume: number) : APiData[] {
  const data : APiData[] = [];
  for (let i = 0; i < volume; i++) {
    data.push({
      uuid: i.toString(),
      firstName: "test-" + i,
      lastName: "test-" + i,
      email: "test-" + i,
      phoneNumber: "test-" + i,
      country: "test-" + i,
      description: "test-" + i,
      isDeleted: false,
      isConfirmed: false,
      createdAt: new Date(),
      updatedAt: new Date(),
    });
  }
  return data;
}

const apiMockData: APiData[] = generateMockData(10);

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ id: string[] }> }
) {
  const id = (await params).id[0];
  const data = apiMockData.find((item) => item.uuid === id);
  if (!data) {
    return NextResponse.json({
      status: 404,
      data: {
        message: "Not Found",
      },
    });
  }
  return NextResponse.json({
    status: 200,
    data: data,
  });
}

export async function PUT(
  request: NextRequest,
  { params }: { params: Promise<{ id: string[] }> }
) {
  const id = (await params).id[0];
  const body = await request.json();
  const idFound = apiMockData.find((item) => item.uuid === id);

  if (!idFound) {
    return NextResponse.json({
      status: 404,
      data: {
        message: "Not Found",
      },
    });
  }
  apiMockData.splice(apiMockData.indexOf(idFound as any), 1);
  apiMockData.push({
    ...idFound,
    firstName: body.name || idFound.firstName,
    description: body.description || idFound.description,
    uuid: id,
    isDeleted: false,
    createdAt: new Date(),
    updatedAt: new Date(),
  });
  return NextResponse.json({
    status: 200,
    data: {
      ...idFound,
      firstName: body.name,
      description: body.description,
    },
  });
}

export async function DELETE(
  request: NextRequest,
  { params }: { params: Promise<{ id: string[] }> }
) {
  const id = (await params).id[0];
  const data = apiMockData.find((item) => item.uuid === id);
  if (!data) {
    return NextResponse.json({
      status: 404,
      data: {
        message: "Not Found",
      },
    });
  }
  apiMockData.splice(apiMockData.indexOf(data as any), 1);
  return NextResponse.json({
    status: 200,
    data: apiMockData.length,
  });
}

export async function PATCH(
  request: NextRequest,
  { params }: { params: Promise<{ id: string[] }> }
) {
  const id = (await params).id[0];
  const body = await request.json();
  const data = apiMockData.find((item) => item.uuid === id);
  if (!data) {
    return NextResponse.json({
      status: 404,
      data: {
        message: "Not Found",
      },
    });
  }
  return NextResponse.json({
    status: 200,
    data: {
      ...data,
      firstName: body.name || data.firstName,
    },
  });
}
