const express = require('express');
const path = require('node:path');
const {
  createContractPdf,
  listTemplates,
} = require('./src/contractService');

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

app.use(express.json({ limit: '1mb' }));
app.use(express.static(path.join(__dirname, 'public')));

app.get('/api/health', (_req, res) => {
  res.json({ ok: true });
});

app.get('/api/templates', async (_req, res, next) => {
  try {
    res.json({ templates: await listTemplates() });
  } catch (error) {
    next(error);
  }
});

app.post('/api/contracts/pdf', async (req, res) => {
  try {
    const result = await createContractPdf({
      template: req.body.template,
      monthlyRent: req.body.monthlyRent,
      paymentDay: req.body.paymentDay,
      deposit: req.body.deposit,
    });

    res.setHeader('Content-Type', 'application/pdf');
    res.setHeader(
      'Content-Disposition',
      `attachment; filename="rental-contract.pdf"; filename*=UTF-8''${encodeURIComponent(result.fileName)}`,
    );
    res.setHeader('Cache-Control', 'no-store');
    res.send(result.buffer);
  } catch (error) {
    console.error(error);
    const status = error.exposeToClient ? 400 : 500;
    res.status(status).json({
      error: error.exposeToClient
        ? error.message
        : 'PDF 產生失敗，請確認 LibreOffice 已安裝且範本格式正確。',
    });
  }
});

app.use((error, _req, res, _next) => {
  console.error(error);
  res.status(500).json({ error: '伺服器發生錯誤。' });
});

app.listen(port, () => {
  console.log(`Rental contract PDF app is running at http://localhost:${port}`);
});
