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

// Create a new form
export const createForm = async (req, res) => {
  try {
    const { form_data, client_id, treelogy_id } = req.body;

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

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

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

    if (clientExists.status !== 1) {
      return errorResponse(res, 'Client is not active', 400);
    }

    const newForm = await Form.create({
      form_data,
      created_by: client_id,
      treelogy_id: treelogy_id || null,
      created_date: new Date(),
      updated_date: new Date()
    });

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

// Get form by ID with client details
export const getFormById = async (req, res) => {
  try {
    const { id } = req.params;

    const form = await Form.findByPk(id, {
      include: [{
        model: Client,
        as: 'creator',
        attributes: ['id', 'client_name', 'email', 'mobile', 'status']
      }]
    });

    if (!form) return errorResponse(res, 'Form not found', 404);

    return successResponse(res, 200, 'OK', form);
  } catch (error) {
    return errorResponse(res, 'Error fetching form', 500, error.message);
  }
};

// Get all forms by client
export const getFormsByClient = async (req, res) => {
  try {
    const { client_id } = req.params;

    const forms = await Form.findAll({
      where: { created_by: client_id },
      include: [{
        model: Client,
        as: 'creator',
        attributes: ['id', 'client_name', 'email']
      }],
      order: [['created_date', 'DESC']]
    });

    return successResponse(res, 200, 'OK', forms);
  } catch (error) {
    return errorResponse(res, 'Error fetching forms', 500, error.message);
  }
};

// Update form
export const updateForm = async (req, res) => {
  try {
    const { id } = req.params;
    const { form_data } = req.body;

    if (!form_data) return errorResponse(res, 'form_data is required', 400);

    const form = await Form.findByPk(id);
    if (!form) return errorResponse(res, 'Form not found', 404);

    form.form_data = form_data;
    form.updated_date = new Date();
    await form.save();

    return successResponse(res, 200, 'Form updated successfully', form);
  } catch (error) {
    return errorResponse(res, 'Error updating form', 500, error.message);
  }
};

// Delete form
export const deleteForm = async (req, res) => {
  try {
    const { id } = req.params;

    const form = await Form.findByPk(id);
    if (!form) return errorResponse(res, 'Form not found', 404);

    await form.destroy();
    return successResponse(res, 200, 'Form deleted successfully', { id });
  } catch (error) {
    return errorResponse(res, 'Error deleting form', 500, error.message);
  }
};





