const express = require('express');
const app = express();
const cors = require('cors');
const allowedOrigins = [
  'http://localhost:3001',
  'http://localhost:3000',
  'https://accounts.maanrishfaxyz.xyz',
  'https://api.maanrishfaxyz.xyz',
];

app.use(cors({
  origin: function (origin, callback) {
    // Allow requests with no origin (like mobile apps, curl, etc.)
    if (!origin) return callback(null, true);
    if (allowedOrigins.includes(origin)) {
      return callback(null, true);
    } else {
      return callback(new Error('Not allowed by CORS'));
    }
  },
  credentials: true
}));
const userRoutes = require('./routes/userRoutes');
const itemRoutes = require('./routes/itemRoutes');
const supplierRoutes = require('./routes/supplierRoutes');
const customerRoutes = require('./routes/customerRoutes');
const purchaseRoutes = require('./routes/purchaseRoutes');
const saleRoutes = require('./routes/saleRoutes');
const expenseRoutes = require('./routes/expenseRoutes');
const bankRoutes = require('./routes/bankRoutes');
const bankTransactionRoutes = require('./routes/bankTransactionRoutes');
const accountRoutes = require('./routes/accountRoutes');
const reportRoutes = require('./routes/reportRoutes');
const dashboardRoutes = require('./routes/dashboardRoutes');
const ledgerRoutes = require('./routes/accountsLedger');
const paymentRoutes = require('./routes/customerPayments');
const invoiceRoutes = require('./routes/invoiceRoutes');
const metaRoutes = require('./routes/metaRoutes.js');
const templateRoutes = require('./routes/templateRoutes');
const rolesRoutes = require('./routes/rolesRoutes');
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
// app.use(express.json());
app.use('/api/users', userRoutes);
app.use('/api/items', itemRoutes);
app.use('/api/suppliers', supplierRoutes);
app.use('/api/customers', customerRoutes);
app.use('/api/purchases', purchaseRoutes);
app.use('/api/sales', saleRoutes);
app.use('/api/expenses', expenseRoutes);
app.use('/api/banks', bankRoutes);
app.use('/api/bank-transactions', bankTransactionRoutes);
app.use('/api/accounts', accountRoutes);
app.use('/api/reports', reportRoutes);
app.use('/api/dashboard', dashboardRoutes);
app.use('/api/accounts/ledger', ledgerRoutes);
app.use('/api/customers/payments', paymentRoutes);
app.use('/api/invoices', invoiceRoutes);
app.use('/api/meta', metaRoutes);
app.use('/api/templates', templateRoutes);
app.use('/api/roles', rolesRoutes);

app.listen(3000, () => console.log('Server running on port 3000'));
