import Dbname from '../models/dbname.js';
import Treelogy from '../models/treelogy.js';
import { successResponse, errorResponse } from '../utils/response.js';

export async function createDbname(req, res) {
  try {
    const { treelogy_id, dbname, locked, status } = req.body;
    if (!treelogy_id || !dbname || !locked) {
      return errorResponse(res, 400, 'treelogy_id, dbname, locked are required');
    }

    const t = await Treelogy.findByPk(treelogy_id);
    if (!t) return errorResponse(res, 404, 'Treelogy not found');

    // Optional uniqueness check if 1:1 desired
    // const exists = await Dbname.findOne({ where: { treelogy_id } });
    // if (exists) return errorResponse(res, 409, 'Dbname already exists for this treelogy');

    const newDbname = await Dbname.create({
      treelogy_id,
      dbname,
      locked,
      status: status ?? 0
    });

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

export async function getAllDbnames(_req, res) {
  try {
    const dbnames = await Dbname.findAll({
      include: [{ model: Treelogy, foreignKey: 'treelogy_id' }]
    });
    return successResponse(res, 200, 'Dbnames fetched successfully', dbnames);
  } catch (error) {
    return errorResponse(res, 500, 'Error fetching dbnames', error.message);
  }
}

export async function getDbnameById(req, res) {
  try {
    const { id } = req.params;
    const dbname = await Dbname.findByPk(id, { include: Treelogy });
    if (!dbname) return errorResponse(res, 404, 'Dbname not found');
    return successResponse(res, 200, 'Dbname fetched successfully', dbname);
  } catch (error) {
    return errorResponse(res, 500, 'Error fetching dbname', error.message);
  }
}

export async function updateDbname(req, res) {
  try {
    const { id } = req.params;
    const { treelogy_id, dbname, locked, status } = req.body;

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

    // Validate foreign key if changed
    if (treelogy_id && treelogy_id !== row.treelogy_id) {
      const exists = await Treelogy.findByPk(treelogy_id);
      if (!exists) return errorResponse(res, 404, 'Treelogy not found');
    }

    row.treelogy_id = treelogy_id ?? row.treelogy_id;
    row.dbname = dbname ?? row.dbname;
    row.locked = locked ?? row.locked;
    row.status = status ?? row.status;

    await row.save();
    return successResponse(res, 200, 'Dbname updated successfully', row);
  } catch (error) {
    return errorResponse(res, 500, 'Error updating dbname', error.message);
  }
}

export async function deleteDbname(req, res) {
  try {
    const { id } = req.params;
    const row = await Dbname.findByPk(id);
    if (!row) return errorResponse(res, 404, 'Dbname not found');
    await row.destroy();
    return successResponse(res, 200, 'Dbname deleted successfully');
  } catch (error) {
    return errorResponse(res, 500, 'Error deleting dbname', error.message);
  }
}
