import type { NextApiResponse } from 'next';
import type { AuthenticatedRequest } from '../../../../lib/middleware/authMiddleware';
import { requireAuth } from '../../../../lib/middleware/authMiddleware';
import { getServerConfig } from '../../../../lib/serverConfig';
import { MaterialService } from '../../../../backend/services/materialService';
import formidable, { Files, File } from 'formidable';
import fs from 'fs';
import path from 'path';

export const config = {
  api: {
    bodyParser: false,
  },
};

export default async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
  if (req.method !== 'POST') {
    return res.status(405).json({ success: false, error: 'Method not allowed' });
  }

  const isAuthenticated = await requireAuth(req, res);
  if (!isAuthenticated) return;

  const { uploadMaxMB } = getServerConfig();
  const maxBytes = uploadMaxMB * 1024 * 1024;

  const form = formidable({
    maxFileSize: maxBytes,
    multiples: false,
    filter: ({ mimetype, originalFilename }) => {
      return (mimetype === 'application/zip' || (originalFilename || '').toLowerCase().endsWith('.zip'));
    },
  });

  const getFirstFile = (files: Files | undefined): File | null => {
    if (!files) return null;
    const entries = Object.values(files || {});
    if (!entries.length) return null;
    const candidate = entries[0] as any;
    if (Array.isArray(candidate)) return candidate[0] || null;
    return candidate || null;
  };

  try {
    const parsed: { fields: any; files: Files } = await new Promise((resolve, reject) => {
      form.parse(req as any, (err, fields, files) => {
        if (err) reject(err);
        else resolve({ fields, files });
      });
    });
    const { fields, files } = parsed;
    const file = getFirstFile(files);
    if (!file) return res.status(400).json({ success: false, error: 'No file provided' });
    if ((file.size || 0) > maxBytes) return res.status(413).json({ success: false, error: `File too large. Max ${uploadMaxMB}MB` });

    const uploadsDir = path.join(process.cwd(), getServerConfig().uploadDir);
    if (!fs.existsSync(uploadsDir)) fs.mkdirSync(uploadsDir, { recursive: true });

    const filename = `zip_${Date.now()}_${path.basename(file.originalFilename || 'asset.zip')}`;
    const destPath = path.join(uploadsDir, filename);
    const srcPath = (file as any).filepath || (file as any).path;
    if (!srcPath) return res.status(400).json({ success: false, error: 'Invalid upload temp path' });
    await fs.promises.copyFile(srcPath, destPath);

    const contentUrl = `/api/v1/files/${filename}`;

    const newMaterial = await MaterialService.createMaterial(req.user!.id, {
      title: fields?.title?.toString() || 'ZIP Asset',
      description: fields?.description?.toString() || 'Uploaded ZIP asset',
      type: 'ASSET_ZIP',
      contentUrl,
      tags: (fields?.tags?.toString() || '').split(',').filter(Boolean),
    });

    return res.status(201).json({ success: true, data: newMaterial });
  } catch (error: any) {
    console.error('Upload ZIP error:', error);
    return res.status(500).json({ success: false, error: error.message || 'Upload failed' });
  }
}
