import express, { Request, Response } from 'express';
import cors from 'cors';
import dotenv from 'dotenv';
import path from 'path';
import fs from 'fs';
import sqlite3 from 'sqlite3';

dotenv.config();

const app = express();
app.use(cors());
app.use(express.json());

const dbPath = path.join(__dirname, '..', '..', 'data', 'app.db');
const db = new sqlite3.Database(dbPath);

// iPad routes
app.post('/api/ipads', (req: Request, res: Response) => {
  const { asset_tag, model } = req.body;
  const stmt = db.prepare('INSERT INTO iPads (asset_tag, model) VALUES (?, ?)');
  stmt.run([asset_tag, model], function (err) {
    if (err) {
      return res.status(400).json({ error: err.message });
    }
    res.status(201).json({ id: this.lastID, asset_tag, model });
  });
});

app.get('/api/ipads', (req: Request, res: Response) => {
  db.all('SELECT * FROM iPads', (err, rows) => {
    if (err) return res.status(500).json({ error: err.message });
    res.json(rows);
  });
});

app.get('/api/ipads/:id', (req: Request, res: Response) => {
  const id = req.params.id;
  db.get('SELECT * FROM iPads WHERE id = ?', [id], (err, row) => {
    if (err) return res.status(500).json({ error: err.message });
    if (!row) return res.status(404).json({ error: 'iPad not found' });
    res.json(row);
  });
});

app.put('/api/ipads/:id', (req: Request, res: Response) => {
  const id = req.params.id;
  const { asset_tag, model } = req.body;
  const stmt = db.prepare('UPDATE iPads SET asset_tag = ?, model = ? WHERE id = ?');
  stmt.run([asset_tag, model, id], function (err) {
    if (err) return res.status(400).json({ error: err.message });
    if (this.changes === 0) return res.status(404).json({ error: 'iPad not found' });
    res.json({ id, asset_tag, model });
  });
});

app.delete('/api/ipads/:id', (req: Request, res: Response) => {
  const id = req.params.id;
  const stmt = db.prepare('DELETE FROM iPads WHERE id = ?');
  stmt.run([id], function (err) {
    if (err) return res.status(500).json({ error: err.message });
    if (this.changes === 0) return res.status(404).json({ error: 'iPad not found' });
    res.status(204).send();
  });
});

// repair routes
app.post('/api/repairs', (req: Request, res: Response) => {
  const { i_pad_id, repair_type, cost, repair_date } = req.body;
  const stmt = db.prepare('INSERT INTO repairs (i_pad_id, repair_type, cost, repair_date) VALUES (?, ?, ?, ?)');
  stmt.run([i_pad_id, repair_type, cost, repair_date], function (err) {
    if (err) return res.status(400).json({ error: err.message });
    res.status(201).json({ id: this.lastID, i_pad_id, repair_type, cost, repair_date });
  });
});

app.get('/api/repairs', (req: Request, res: Response) => {
  db.all('SELECT * FROM repairs', (err, rows) => {
    if (err) return res.status(500).json({ error: err.message });
    res.json(rows);
  });
});

app.get('/api/repairs/:id', (req: Request, res: Response) => {
  const id = req.params.id;
  db.get('SELECT * FROM repairs WHERE id = ?', [id], (err, row) => {
    if (err) return res.status(500).json({ error: err.message });
    if (!row) return res.status(404).json({ error: 'Repair not found' });
    res.json(row);
  });
});

app.put('/api/repairs/:id', (req: Request, res: Response) => {
  const id = req.params.id;
  const { i_pad_id, repair_type, cost, repair_date } = req.body;
  const stmt = db.prepare('UPDATE repairs SET i_pad_id = ?, repair_type = ?, cost = ?, repair_date = ? WHERE id = ?');
  stmt.run([i_pad_id, repair_type, cost, repair_date, id], function (err) {
    if (err) return res.status(400).json({ error: err.message });
    if (this.changes === 0) return res.status(404).json({ error: 'Repair not found' });
    res.json({ id, i_pad_id, repair_type, cost, repair_date });
  });
});

app.delete('/api/repairs/:id', (req: Request, res: Response) => {
  const id = req.params.id;
  const stmt = db.prepare('DELETE FROM repairs WHERE id = ?');
  stmt.run([id], function (err) {
    if (err) return res.status(500).json({ error: err.message });
    if (this.changes === 0) return res.status(404).json({ error: 'Repair not found' });
    res.status(204).send();
  });
});

const port = process.env.PORT ? parseInt(process.env.PORT) : 3000;
app.listen(port, () => console.log(`Server running on port ${port}`));
