import {
  createJobSchema,
  updateJobSchema,
  normalizeJobPayload,
} from "../models/job.schema.js";
import { listJobs, getJobById, createJob, updateJob, deleteJob } from "../services/jobs.service.js";
import { successResponse, errorResponse } from "../utils/response.js";
import { HTTP_STATUS } from "../constants/index.js";
import logger from "../utils/logger.js";

export const list = async (req, res, next) => {
  try {
    const items = await listJobs();
    return successResponse(res, items, HTTP_STATUS.OK);
  } catch (err) {
    logger.error("Error listing jobs:", err);
    next(err);
  }
};

export const getById = async (req, res, next) => {
  try {
    const { id } = req.params;
    if (!id) {
      return errorResponse(res, "Job ID is required", HTTP_STATUS.BAD_REQUEST);
    }
    const item = await getJobById(id);
    if (!item) {
      return errorResponse(res, "Job not found", HTTP_STATUS.NOT_FOUND);
    }
    return successResponse(res, item, HTTP_STATUS.OK);
  } catch (err) {
    logger.error("Error getting job by ID:", err);
    next(err);
  }
};

export const create = async (req, res, next) => {
  try {
    const normalized = normalizeJobPayload(req.body);
    const parsed = createJobSchema.safeParse(normalized);
    if (!parsed.success) {
      return errorResponse(
        res,
        "Validation failed",
        HTTP_STATUS.BAD_REQUEST,
        parsed.error.flatten()
      );
    }
    const created = await createJob(parsed.data);
    logger.info(`Job created with ID: ${created.id || created._id}`);
    return successResponse(res, created, HTTP_STATUS.CREATED);
  } catch (err) {
    logger.error("Error creating job:", err);
    next(err);
  }
};

export const update = async (req, res, next) => {
  try {
    const { id } = req.params;
    if (!id) {
      return errorResponse(res, "Job ID is required", HTTP_STATUS.BAD_REQUEST);
    }
    const normalized = normalizeJobPayload(req.body);
    const parsed = updateJobSchema.safeParse(normalized);
    if (!parsed.success) {
      return errorResponse(
        res,
        "Validation failed",
        HTTP_STATUS.BAD_REQUEST,
        parsed.error.flatten()
      );
    }
    const updated = await updateJob(id, parsed.data);
    if (!updated) {
      return errorResponse(res, "Job not found", HTTP_STATUS.NOT_FOUND);
    }
    logger.info(`Job updated with ID: ${id}`);
    return successResponse(res, updated, HTTP_STATUS.OK);
  } catch (err) {
    logger.error("Error updating job:", err);
    next(err);
  }
};

export const remove = async (req, res, next) => {
  try {
    const { id } = req.params;
    if (!id) {
      return errorResponse(res, "Job ID is required", HTTP_STATUS.BAD_REQUEST);
    }
    const ok = await deleteJob(id);
    if (!ok) {
      return errorResponse(res, "Job not found", HTTP_STATUS.NOT_FOUND);
    }
    logger.info(`Job deleted with ID: ${id}`);
    return res.status(HTTP_STATUS.NO_CONTENT).send();
  } catch (err) {
    logger.error("Error deleting job:", err);
    next(err);
  }
};



