const express = require('express');
const cors = require('cors');
const { Pool } = require('pg');
const { createClient } = require('redis');
const cookieParser = require('cookie-parser');
const fs = require('fs');
const path = require('path');
require('dotenv').config();

const app = express();
const port = process.env.PORT || 4001;

const allowedOrigins = [
  'http://localhost:4000',
  'https://qr.houseofwebsites.ai',
  process.env.FRONTEND_URL
].filter(Boolean);

app.use(cors({ origin: allowedOrigins, credentials: true }));
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));
app.use(cookieParser());

// Routes
const authRoutes = require('./routes/auth');
const qrRoutes = require('./routes/qr');
const redirectRoutes = require('./routes/redirect');
const analyticsRoutes = require('./routes/analytics');
const adminRoutes = require('./routes/admin');
const paymentsRoutes = require('./routes/payments');

app.use('/api/auth', authRoutes);
app.use('/api/qr', qrRoutes);
app.use('/api/analytics', analyticsRoutes);
app.use('/api/admin', adminRoutes);
app.use('/api/payments', paymentsRoutes);

// Support Traefik stripped prefixes
app.use('/auth', authRoutes);
app.use('/qr', qrRoutes);
app.use('/analytics', analyticsRoutes);
app.use('/admin', adminRoutes);
app.use('/payments', paymentsRoutes);

app.use('/l', redirectRoutes);

// Postgres setup
const pgPool = new Pool({
  host: process.env.PGHOST || 'postgres',
  port: process.env.PGPORT || 5432,
  user: process.env.PGUSER || 'postgres',
  password: process.env.PGPASSWORD || 'postgres',
  database: process.env.PGDATABASE || 'ckqr',
});

// Initialize Database automatically
const initDb = async () => {
  try {
    const sql = fs.readFileSync(path.join(__dirname, 'db', 'init.sql'), 'utf8');
    await pgPool.query(sql);
    console.log('Database initialized successfully.');
    
    // Force promote the owner to admin (enterprise role)
    await pgPool.query("UPDATE users SET role = 'enterprise' WHERE email = 'mohan.k@cavininfotech.com'");
    await pgPool.query("UPDATE users SET role = 'enterprise' WHERE email = 'admin@cavininfotech.com'");
    await pgPool.query("UPDATE users SET role = 'enterprise' WHERE role = 'admin'");
  } catch (err) {
    console.error('Database initialization failed:', err);
  }
};
initDb();

// Redis setup
const redisClient = createClient({
  url: `redis://${process.env.REDIS_HOST || 'redis'}:${process.env.REDIS_PORT || 6379}`
});

redisClient.on('error', (err) => console.log('Redis Client Error', err));

const healthCheck = async (req, res) => {
  let pgStatus = 'Unknown';
  let redisStatus = 'Unknown';

  try {
    const pgRes = await pgPool.query('SELECT NOW()');
    pgStatus = 'Connected';
  } catch (err) {
    pgStatus = `Error: ${err.message}`;
  }

  try {
    if (!redisClient.isOpen) {
      await redisClient.connect();
    }
    await redisClient.ping();
    redisStatus = 'Connected';
  } catch (err) {
    redisStatus = `Error: ${err.message}`;
  }

  res.json({
    status: 'ok',
    services: {
      api: 'Running',
      postgres: pgStatus,
      redis: redisStatus
    }
  });
};

app.get('/api/health', healthCheck);
app.get('/health', healthCheck);

// Auto-migrate schema
pgPool.query(`ALTER TABLE qr_codes ADD COLUMN IF NOT EXISTS name VARCHAR(255);`).catch(console.error);
pgPool.query(`
CREATE TABLE IF NOT EXISTS qr_scans (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    qr_code_id UUID REFERENCES qr_codes(id) ON DELETE CASCADE,
    ip_address VARCHAR(45),
    user_agent TEXT,
    os VARCHAR(50),
    device_type VARCHAR(50),
    browser VARCHAR(50),
    country VARCHAR(100),
    city VARCHAR(100),
    scanned_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);`).catch(console.error);

app.listen(port, () => {
  console.log(`CK-QR API listening on port ${port}`);
});
