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

const API_BASE =
  "https://api.astrology.togwe.com/api/user/horoscope";

const allowedPeriods = [
  "daily",
  "weekly",
  "monthly",
  "yearly",
  "daily-moon",
  "daily-nakshatra",
  "feedback",
];

export async function POST(
  request: NextRequest,
  context: {
    params: Promise<{
      period: string;
    }>;
  }
) {
  try {
    const { period } = await context.params;

    if (!allowedPeriods.includes(period)) {
      return NextResponse.json(
        {
          status: false,
          message: "Invalid horoscope type.",
        },
        {
          status: 400,
        }
      );
    }

    const body = await request.json();

    const authorization =
      request.headers.get("authorization");

    if (!authorization) {
      return NextResponse.json(
        {
          status: false,
          message: "Authentication token is required.",
        },
        {
          status: 401,
        }
      );
    }

    const response = await fetch(
      `${API_BASE}/${period}`,
      {
        method: "POST",

        headers: {
          "Content-Type": "application/json",
          Accept: "application/json",
          Authorization: authorization,
        },

        body: JSON.stringify(body),

        cache: "no-store",
      }
    );

    const data = await response.json();

    return NextResponse.json(
      data,
      {
        status: response.status,
      }
    );
  } catch (error) {
    console.error(
      "Horoscope proxy error:",
      error
    );

    return NextResponse.json(
      {
        status: false,
        message: "Unable to connect to horoscope API.",
      },
      {
        status: 500,
      }
    );
  }
}