import { BlogModel } from "../models/blog.model.js";
import logger from "../utils/logger.js";

export async function listBlogs() {
  try {
    const items = await BlogModel.find({}).sort({ createdAt: -1 }).lean();
    return items;
  } catch (error) {
    logger.error("Error in listBlogs service:", error);
    throw error;
  }
}

export async function getBlogById(id) {
  try {
    if (!id) {
      throw new Error("Blog ID is required");
    }
    const item = await BlogModel.findById(id).lean();
    return item || null;
  } catch (error) {
    logger.error("Error in getBlogById service:", error);
    throw error;
  }
}

export async function getBlogByCategory(category) {
  try {
    if (!category) {
      throw new Error("Blog category is required");
    }
    const item = await BlogModel.findOne({ category }).lean();
    return item || null;
  } catch (error) {
    logger.error("Error in getBlogByCategory service:", error);
    throw error;
  }
}

export async function createBlog(data) {
  try {
    const created = await BlogModel.create(data);
    return created.toObject();
  } catch (error) {
    logger.error("Error in createBlog service:", error);
    throw error;
  }
}

export async function updateBlog(id, data) {
  try {
    if (!id) {
      throw new Error("Blog ID is required");
    }
    const updated = await BlogModel.findByIdAndUpdate(id, { $set: data }, { new: true }).lean();
    return updated || null;
  } catch (error) {
    logger.error("Error in updateBlog service:", error);
    throw error;
  }
}

export async function updateBlogByCategory(category, data) {
  try {
    if (!category) {
      throw new Error("Blog category is required");
    }
    const updated = await BlogModel.findOneAndUpdate({ category }, { $set: data }, { new: true, upsert: true }).lean();
    return updated || null;
  } catch (error) {
    logger.error("Error in updateBlogByCategory service:", error);
    throw error;
  }
}

export async function deleteBlog(id) {
  try {
    if (!id) {
      throw new Error("Blog ID is required");
    }
    const res = await BlogModel.findByIdAndDelete(id).lean();
    return !!res;
  } catch (error) {
    logger.error("Error in deleteBlog service:", error);
    throw error;
  }
}

export async function updateComponentByCategory(category, componentId, componentData) {
  try {
    if (!category) {
      throw new Error("Blog category is required");
    }
    if (!componentId) {
      throw new Error("Component ID is required");
    }
    
    const blog = await BlogModel.findOne({ category });
    if (!blog) {
      throw new Error("Blog not found");
    }
    
    const componentIndex = blog.components.findIndex(
      (comp) => comp._id.toString() === componentId
    );
    
    if (componentIndex === -1) {
      throw new Error("Component not found");
    }
    
    // Update the specific component
    blog.components[componentIndex] = {
      ...blog.components[componentIndex].toObject(),
      ...componentData,
      _id: blog.components[componentIndex]._id,
    };
    
    await blog.save();
    return blog.toObject();
  } catch (error) {
    logger.error("Error in updateComponentByCategory service:", error);
    throw error;
  }
}

export async function updateHeroSectionByCategory(category, heroData) {
  try {
    if (!category) {
      throw new Error("Blog category is required");
    }
    
    const updated = await BlogModel.findOneAndUpdate(
      { category },
      { $set: { heroSection: heroData } },
      { new: true, upsert: true }
    ).lean();
    
    return updated || null;
  } catch (error) {
    logger.error("Error in updateHeroSectionByCategory service:", error);
    throw error;
  }
}

