import express, {Request} from 'express';
import {listWorkspaces, openWithFinder, openWithCode, cloneApp, getAppLocation, getLocalBundles} from '../controllers/workspace';
import bodyParser from 'body-parser'
import { closeAppByLocation, insertOrUpdateOpenapp } from '../database/cliconfig';
export const jsonParser = bodyParser.json();
const router = express.Router();
// import workspaceMiddleware from '../middlewares/workspace';

// router.get('/workspaces', workspaceMiddleware, async (req, res) => {
//   
// });


router.delete('/workspaces/:workspaceId/closeApp', jsonParser, async (req, res, next) => {
  const fullPath = req.body.fullPath;
  closeAppByLocation(fullPath);
  res.status(200);
  res.end();
});

router.post('/operation/opencode/:location', async (req, res, next) => {
  try {
    const location = decodeURIComponent(req.params.location);
    await openWithCode(location);
    res.status(200);
    res.end();
  } catch (err) {
    console.error("Failed when trying to open location with finder");
    next(err);
  }
});

router.post('/operation/openfinder/:location', async (req, res, next) => {
  try {
    const location = decodeURIComponent(req.params.location);
    await openWithFinder(location);
    res.status(200);
    res.end();
  } catch (err) {
    console.error("Failed when trying to open location with finder");
    next(err);
  }
});

router.get('/workspaces/list', async (req, res, next) => {
  try {
    const response = await listWorkspaces();
    res.json(response);
  } catch (err: any) {
    console.error("Failure in GET /workspace");
    next(err);
  }
});

router.post('/workspaces', async (req, res) => {
  // add entry in workspace table to link a filesystem path and name
  // link workspace to apptile-server url
  // clone sdk repository and checkout specified branch
});

router.post('/workspaces/:workspaceId/cloneApp', jsonParser, async (req, res, next) => {
  try {
    const workspace = parseInt(req.params.workspaceId);
    // TODO(add types to manifest and appIntegrations)
    const manifest = req.body.manifest;
    const appIntegrations = req.body.appIntegrations;
    const apptileConfig = req.body.apptileConfig;
    cloneApp(workspace, manifest, appIntegrations, apptileConfig);
    res.status(200);
    res.end();
  } catch (err) {
    console.error("Failed to clone app");
    next(err);
  }
});

router.get('/workspaces/:workspaceId/app/:appId', async (req, res, next) => {
  const workspaceId = req.params.workspaceId;
  const appId = req.params.appId;
  try {
    const appLocation = await getAppLocation(parseInt(workspaceId), appId);
    const localBundles = await getLocalBundles(appLocation);
    
    res.json({
      appId,
      appRoot: appLocation,
      codePushBundles: localBundles
    });
  } catch (err) {
    next(err);
  }
});

router.post('/workspaces/:workspaceId/openApp/:appId', async (req, res, next) => {
  // find the folder for appid in the workspace
  const appId = req.params.appId;
  const workspaceId = req.params.workspaceId;

  try {
    const appRoot = await getAppLocation(parseInt(workspaceId), appId);
    insertOrUpdateOpenapp(workspaceId, appId, appRoot);
    res.status(201);
    res.end();
  } catch (err) {
    console.error("Failed to open app");
    next(err);
  }
});

export default router;
