const Project = require('../models/project');
const bcrypt = require('bcryptjs');
const saltRounds = 10; 
const { spawn } = require('child_process');
const path = require('path');

exports.getProject = async (req, res) => {
  try {
    const projects = await Project.findAll();
    res.status(200).json(projects);
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
};

exports.getProjectByID = async (req, res) => {
  try {
    const project = await Project.findByPk(req.params.id);
    if (project) {
      res.status(200).json(project);
    } else {
      res.status(404).json({ error: 'Project not found' });
    }
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
};



exports.createProject = async (req, res) => {
  try {
    const { project_id, project_name, project_detail, project_startdate, project_stopdate, project_lat, project_long } = req.body;
    const newProject = await Project.create({
        project_id,
        project_name,
        project_detail,
        project_startdate,
        project_stopdate,
        project_lat,
        project_long

    });
    res.status(201).json({ actionStatus: 'Success', message: 'Create Success!!', statusCode: 200, prject: newProject });
  } catch (err) {
    res.status(400).json({ error: err.message });
  }
};

exports.updateProject = async (req, res) => {
    try {
        const { id } = req.params;
        const { project_name, project_detail, project_startdate, project_stopdate, project_lat, project_long } = req.body;

        // Find the project by ID
        const project = await Project.findByPk(id);

        if (project) {
            // Update the project's details
            project.project_name = project_name || project.project_name;
            project.project_detail = project_detail || project.project_detail;
            project.project_startdate = project_startdate || project.project_startdate;
            project.project_stopdate = project_stopdate || project.project_stopdate;
            project.project_lat = project_lat || project.project_lat;
            project.project_long = project_long || project.project_long;
            project.updated_at = new Date(); // Update the timestamp

            // Save the updated project
            await project.save();

            res.status(200).json({
                actionStatus: 'Success',
                message: 'Update Success!!',
                statusCode: 200,
                project: project
            });
        } else {
            res.status(404).json({ error: 'Project not found' });
        }
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
};
 
exports.deleteProject = async (req, res) => {
    try {
      const { id } = req.params;
  
      // Find the project by ID
      const project = await Project.findByPk(id);
  
      if (project) {
        // Delete the project
        await project.destroy();
        res.status(200).json({
          actionStatus: 'Success',
          message: 'Delete Success!!',
          statusCode: 200
        });
      } else {
        res.status(404).json({ error: 'Project not found' });
      }
    } catch (err) {
      res.status(500).json({ error: err.message });
    }
  };
  
