import fastify from 'fastify';
import jwt from '@fastify/jwt';
import dotenv from 'dotenv';
import { prismaPlugin } from './plugins/prisma';
import { authRoutes } from './routes/auth.routes';
import { roomRoutes } from './routes/rooms.routes';
import { batchRoutes } from './routes/batches.routes';
import { timeclockRoutes } from './routes/timeclock.routes';
import { walkthroughRoutes } from './routes/walkthrough.routes';
import { suppliesRoutes } from './routes/supplies.routes';
import { ipmRoutes } from './routes/ipm.routes';
import { touchPointsRoutes } from './routes/touch-points.routes'; // Use new file
import { taskRoutes } from './routes/tasks.routes';
import { taskTemplateRoutes } from './routes/task-templates.routes';
import { roleRoutes } from './routes/roles.routes';
import { userRoutes } from './routes/users.routes';
import { walkthroughSettingsRoutes } from './routes/walkthrough-settings.routes';
import { weightLogsRoutes } from './routes/weight-logs.routes';
import { ipmSchedulesRoutes } from './routes/ipm-schedules.routes';
import { analyticsRoutes } from './routes/analytics.routes';
import { layoutRoutes } from './routes/layout.routes';
import { metrcRoutes } from './routes/metrc.routes';
import { visitorRoutes } from './routes/visitors.routes';
import { accessZoneRoutes } from './routes/access-zones.routes';
import { messagingRoutes } from './routes/messaging.routes';

dotenv.config();

const server = fastify({
    logger: true
});

// Register Plugins
server.register(prismaPlugin);
server.register(jwt, {
    secret: process.env.JWT_SECRET || 'supersecret'
});

// Register Routes
server.register(authRoutes, { prefix: '/api/auth' });
server.register(roomRoutes, { prefix: '/api/rooms' });
server.register(batchRoutes, { prefix: '/api/batches' });
server.register(weightLogsRoutes, { prefix: '/api/batches' });
server.register(timeclockRoutes, { prefix: '/api/timeclock' });
server.register(walkthroughRoutes, { prefix: '/api/walkthroughs' });
server.register(suppliesRoutes, { prefix: '/api' });
server.register(touchPointsRoutes, { prefix: '' });
server.register(ipmRoutes, { prefix: '' });
server.register(ipmSchedulesRoutes, { prefix: '/api/ipm/schedules' });
server.register(taskRoutes, { prefix: '/api/tasks' });
server.register(taskTemplateRoutes, { prefix: '/api/task-templates' });
server.register(roleRoutes, { prefix: '/api/roles' });
server.register(userRoutes, { prefix: '/api/users' });
server.register(walkthroughSettingsRoutes, { prefix: '/api/walkthrough-settings' });
server.register(analyticsRoutes, { prefix: '/api/analytics' });
server.register(layoutRoutes, { prefix: '/api/layout' });
server.register(metrcRoutes, { prefix: '/api/metrc' });
server.register(visitorRoutes, { prefix: '/api/visitors' });
server.register(accessZoneRoutes, { prefix: '/api/zones' });
server.register(messagingRoutes, { prefix: '/api/messaging' });

// Phase 10: Compliance
import { auditRoutes } from './routes/audit.routes';
import { documentRoutes } from './routes/documents.routes';
server.register(auditRoutes, { prefix: '/api/audit' });
server.register(documentRoutes, { prefix: '/api/documents' });

// Phase 13: Advanced Features
import { environmentRoutes } from './routes/environment.routes';
import { financialRoutes } from './routes/financial.routes';
import { insightsRoutes } from './routes/insights.routes';
import { uploadRoutes } from './routes/upload.routes';
server.register(environmentRoutes, { prefix: '/api/environment' });
server.register(financialRoutes, { prefix: '/api/financial' });
server.register(insightsRoutes, { prefix: '/api/insights' });
server.register(uploadRoutes, { prefix: '/api/upload' });

// Admin routes (demo/testing)
import { adminRoutes } from './routes/admin.routes';
server.register(adminRoutes, { prefix: '/api/admin' });

server.get('/api/healthz', async (request, reply) => {
    return { status: 'ok', timestamp: new Date().toISOString() };
});

const start = async () => {
    try {
        const port = parseInt(process.env.PORT || '3000');
        await server.listen({ port, host: '0.0.0.0' });
        console.log(`Server listening on port ${port}`);
    } catch (err) {
        server.log.error(err);
        process.exit(1);
    }
};

start();
