import fastify from "fastify";
import showLocations from "./clean/usecases/showLocations";
import LocationsRepository from "./clean/entities/locationsRepository";
import FastifyPresenter from "./clean/adapters/fastifyPresenter";
import SqliteLocationsRepository from "./clean/adapters/sqliteLocationsRepository";
import Database from "better-sqlite3";
import createLocation from "./clean/usecases/createLocation";
import getForecasts from "./clean/usecases/getForecasts";
import OpenWeatherService from "./clean/adapters/openWeatherService";
import WeatherService from "./clean/entities/weatherService";

const db = new Database("weathery.db");
const APPID = "d6f0f985f37372e9824c68a52662cc23";

const app = fastify({
  logger: true,
});

// const locationsRepository = new InMemoryLocationRepository([
//   new Location("Rouen", 49.443232, 1.099971),
//   new Location("Bourg-Achard", 49.35322, 0.81623),
// ]);
const locationsRepository: LocationsRepository = new SqliteLocationsRepository(
  db
);
const weatherService: WeatherService = new OpenWeatherService(APPID);

app.get("/locations", async (_, res) => {
  showLocations(locationsRepository, new FastifyPresenter(res));
});

app.get("/", async (_, res) => {
  await getForecasts(
    new FastifyPresenter(res),
    locationsRepository,
    weatherService
  );
});

app.post("/locations", async (req, res) => {
  createLocation(
    locationsRepository,
    new FastifyPresenter(res),
    req.body as any
  );
});

app.listen(8080);
