// app.js
import dotenv from "dotenv";
import express from "express";
import cors from "cors";
import path from "path";
import { fileURLToPath } from "url";
import clientRoutes from "./routes/clientRoutes.js";
import loginRoutes from "./routes/loginRoutes.js";
import mappingRoutes from "./routes/mappingRoutes.js";
import treelogyRoutes from "./routes/treelogyRoutes.js";
import clientwiseFeaturesRoutes from "./routes/clientwiseFeaturesRoutes.js";
import dbnameRoutes from "./routes/dbnameRoutes.js";
import authRoutes from "./routes/authRoutes.js";
import superAdminRoutes from "./routes/superAdminRoutes.js";
import projectRoutes from "./routes/projectRoutes.js";
import meRoutes from "./routes/meRoutes.js";
import dashboardVisibilityRoutes from "./routes/dashboardVisibilityRoutes.js";
import userMasterRoutes from "./routes/userMasterRoutes.js";
import treesRoutes from "./routes/treesRoutes.js";
import surveyRoutes from "./routes/surveyRoutes.js";
import mapRoutes from "./routes/mapRoutes.js";
import formRoutes from "./routes/formRoutes.js";
import mobileRoutes from "./routes/mobileRoutes.js";
import sequelize from "./config/config.js";
import './models/index.js'

dotenv.config();

const app = express();
app.use(cors());
// app.use(express.json());
// app.use(express.urlencoded({ extended: true }));

// Serve static files from uploads directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use('/api/uploads', express.static(path.join(__dirname, 'uploads')));

app.use(express.json({ limit: '20mb' }));
app.use(express.urlencoded({ limit: '20mb', extended: true }));


app.get("/", (req, res) => {
  res.send("Welcome to the Treelogy API testing");
});

//auth routes
app.use("/api/auth", authRoutes);

// Super Admin routes
app.use("/api/superadmin", superAdminRoutes);

// Project routes (alias for treelogy)
app.use("/api/projects", projectRoutes);

// User routes
app.use("/api/me", meRoutes);

// Dashboard visibility routes
app.use("/api/dashboard-visibility", dashboardVisibilityRoutes);

// User master routes (role management)
app.use("/api/user-master", userMasterRoutes);

app.use("/api/clients", clientRoutes);
app.use("/api/login", loginRoutes);
app.use("/api/mapping", mappingRoutes);
app.use("/api/treelogy", treelogyRoutes);
app.use("/api/clientwisefeatures", clientwiseFeaturesRoutes);
app.use("/api/dbname", dbnameRoutes);
app.use("/api/surveys", surveyRoutes);
app.use("/api/map", mapRoutes);
app.use("/api/trees", treesRoutes);
app.use("/api", formRoutes);
app.use("/api/mobile/project", mobileRoutes);


const PORT = process.env.PORT || 5000;

// sequelize.sync({ force: false }) // force: false ensures it doesn't drop existing data
//   .then(() => console.log('Database synced successfully'))
//   .catch(err => console.log('Error syncing database:', err));

app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});
