const express = require('express');
const cors = require('cors');
require('dotenv').config();

const app = express();

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

// Routes
app.use('/api/auth', require('./routes/auth'));
app.use('/api/products', require('./routes/products'));
app.use('/api/customers', require('./routes/customers'));
app.use('/api/sales', require('./routes/sales'));
app.use('/api/reports', require('./routes/reports'));

// Health check
app.get('/api/health', (req, res) => res.json({ status: 'SMARTSHOP API running ✅' }));

// Seed (MongoDB)
require('./config/seed').ensureSeed().catch((err) => {
  console.error('Mongo seed failed:', err);
});

const PORT = process.env.PORT || 5002;
app.listen(PORT, () => {
  console.log(`\n🚀 SMARTSHOP LTD API running on port ${PORT}`);
  console.log(`📦 Environment: ${process.env.NODE_ENV || 'development'}\n`);
});
