import express from "express"
import bodyParser from "body-parser"
import mongoose from "mongoose"
import cors from "cors"
import dotenv from "dotenv"
import helmet from "helmet"
import morgan from "morgan"
import clientRoutes from "./routes/client.js"
import generalRoutes from "./routes/general.js"
import managmentRoutes from "./routes/management.js"
import saleRoutes from "./routes/sales.js"
import User from "./models/User.js"
import {dataUser, dataProduct, dataProductStat, dataTransaction, dataOverallStat} from "./data/index.js"
import Product from "./models/Product.js"
import ProductStat from "./models/ProductStat.js"
import Transaction from "./models/Transaction.js"
import Stat from "./models/Stat.js"

//configuration setup
dotenv.config();
const app = express();
app.use(express.json());
app.use(helmet());
app.use(helmet.crossOriginResourcePolicy({policy: "cross-origin"})); //cross origin sharing request
app.use(morgan("common"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cors());

//routes
app.use("/client", clientRoutes);
app.use("/general", generalRoutes);
app.use("/management", managmentRoutes);
app.use("/sales", saleRoutes);

//mongoose setup
const PORT = process.env.PORT || 9000;
mongoose.connect(process.env.MONGO_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
}).then(() => {
    app.listen(PORT, () => console.log(`Server Port: ${PORT}`));


    //===** one time add to data base **===//
    
    //Product.insertMany(dataProduct);
    //ProductStat.insertMany(dataProductStat);
    //User.insertMany(dataUser);
    //Transaction.insertMany(dataTransaction);
    //Stat.insertMany(dataOverallStat);

}).catch((error) => console.log(`${error} didn't connect`))



