import ClientwiseFeatures from '../models/clientwiseFeatures.js';
import Client from '../models/client.js';
import { successResponse, errorResponse } from '../utils/response.js';

export async function createFeature(req, res) {
  try {
    const {
      client_id,
      feature_export_module = false,
      feature_sms_module = false
    } = req.body;

    if (!client_id) {
      return errorResponse(res, 400, 'client_id is required');
    }

    const client = await Client.findByPk(client_id);
    if (!client) return errorResponse(res, 404, 'Client not found');

    // If you want a single row per client, use upsert + unique index:
    // const [feature] = await ClientwiseFeatures.upsert({
    //   client_id, feature_export_module, feature_sms_module
    // });

    const feature = await ClientwiseFeatures.create({
      client_id,
      feature_export_module,
      feature_sms_module
    });

    return successResponse(res, 201, 'Feature created successfully', feature);
  } catch (error) {
    return errorResponse(res, 500, 'Error creating feature', error.message);
  }
}

export async function getFeaturesByClient(req, res) {
  try {
    const { client_id } = req.params;
    const features = await ClientwiseFeatures.findAll({ where: { client_id } });
    return successResponse(res, 200, 'Features fetched successfully', features);
  } catch (error) {
    return errorResponse(res, 500, 'Error fetching features', error.message);
  }
}
