import { Request, Response } from "express";
import {
  getWheel,
  getWheelEligibility,
  spinWheel,
  getSpinHistory,
  createWheel,
  listWheels,
  updateWheel,
  activateWheel,
  deactivateWheel,
  deleteWheel,
} from "../services/spinWheelService";
import { sendResponse, handleError } from "../utils/apiResponse";

export async function getWheelController(req: Request, res: Response) {
  try {
    const wheel = await getWheel();

    sendResponse(res, 200, "Wheel fetched", { wheel });
  } catch (error) {
    handleError(res, error);
  }
}

export async function getWheelEligibilityController(
  req: Request,
  res: Response,
) {
  try {
    const userId = req.user!.id;

    const eligibility = await getWheelEligibility(userId);

    sendResponse(res, 200, "Wheel eligibility fetched", { eligibility });
  } catch (error) {
    handleError(res, error);
  }
}

export async function spinWheelController(req: Request, res: Response) {
  try {
    const userId = req.user!.id;

    const result = await spinWheel(userId);

    sendResponse(res, 200, "Wheel spun successfully", result);
  } catch (error) {
    handleError(res, error);
  }
}

export async function getSpinHistoryController(req: Request, res: Response) {
  try {
    const userId = req.user!.id;

    const history = await getSpinHistory(userId);

    sendResponse(res, 200, "Spin history fetched", { history });
  } catch (error) {
    handleError(res, error);
  }
}

export async function createWheelController(req: Request, res: Response) {
  try {
    const { name, segments, isActive } = req.body;

    if (!name || !Array.isArray(segments) || segments.length === 0) {
      sendResponse(res, 400, "name and segments are required");
      return;
    }

    const wheel = await createWheel({
      name,
      segments,
      isActive,
    });

    sendResponse(res, 201, "Wheel created", { wheel });
  } catch (error) {
    handleError(res, error);
  }
}

export async function listWheelsController(req: Request, res: Response) {
  try {
    const wheels = await listWheels();
    sendResponse(res, 200, "Wheels fetched", { wheels });
  } catch (error) {
    handleError(res, error);
  }
}

export async function updateWheelController(req: Request, res: Response) {
  try {
    const { wheelId } = req.params;

    if (typeof wheelId !== "string") {
      sendResponse(res, 400, "Invalid wheelId");
      return;
    }

    const { name, segments, isActive } = req.body;

    const wheel = await updateWheel(wheelId, {
      name,
      segments,
      isActive,
    });

    sendResponse(res, 200, "Wheel updated", { wheel });
  } catch (error) {
    handleError(res, error);
  }
}

export async function activateWheelController(req: Request, res: Response) {
  try {
    const { wheelId } = req.params;

    if (typeof wheelId !== "string") {
      sendResponse(res, 400, "Invalid wheelId");
      return;
    }

    const wheel = await activateWheel(wheelId);
    sendResponse(res, 200, "Wheel activated", { wheel });
  } catch (error) {
    handleError(res, error);
  }
}

export async function deactivateWheelController(req: Request, res: Response) {
  try {
    const { wheelId } = req.params;

    if (typeof wheelId !== "string") {
      sendResponse(res, 400, "Invalid wheelId");
      return;
    }

    const wheel = await deactivateWheel(wheelId);
    sendResponse(res, 200, "Wheel deactivated", { wheel });
  } catch (error) {
    handleError(res, error);
  }
}

export async function deleteWheelController(req: Request, res: Response) {
  try {
    const { wheelId } = req.params;

    if (typeof wheelId !== "string") {
      sendResponse(res, 400, "Invalid wheelId");
      return;
    }

    const result = await deleteWheel(wheelId);
    sendResponse(res, 200, "Wheel deleted", result);
  } catch (error) {
    handleError(res, error);
  }
}
