/*
 * Copyright (C) 2026  Tim Zimmermann <tim@aglais.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.aglais.zms.server.route;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpRequest;
import org.aglais.zms.api.core.model.SpeciesAncestryData;
import org.aglais.zms.db.core.Database;
import org.aglais.zms.db.core.model.Species;
import org.aglais.zms.server.routing.HttpMethod;
import org.aglais.zms.server.routing.HttpRoute;

import static org.aglais.zms.api.core.APIConstants.*;
import static org.aglais.zms.server.route.util.RouteUtil.readJson;
import static org.aglais.zms.server.route.util.RouteUtil.writeJson;

/**
 * Route handler for /db/species/* endpoints
 */
public class DbSpeciesRoute {

    private final Database db;

    /**
     * Create a new species route handler
     *
     * @param db The {@link Database} to operate on
     */
    public DbSpeciesRoute(Database db) {
        this.db = db;
    }

    @HttpRoute(path = URL_DB_ADD_SPECIES, method = HttpMethod.POST)
    public void addSpecies(ChannelHandlerContext ctx, HttpRequest request, FullHttpResponse response) {
        writeJson(response.content(), db.addSpecies(readJson(request, Species.class)));
    }

    @HttpRoute(path = URL_DB_ALL_SPECIES)
    public void allSpecies(ChannelHandlerContext ctx, HttpRequest request, FullHttpResponse response) {
        writeJson(response.content(), db.getAllSpecies());
    }

    @HttpRoute(path = URL_DB_DELETE_SPECIES, method = HttpMethod.POST)
    public void deleteSpecies(ChannelHandlerContext ctx, HttpRequest request, FullHttpResponse response) {
        writeJson(response.content(), db.deleteSpecies(readJson(request, Species.class)));
    }

    @HttpRoute(path = URL_DB_UPDATE_SPECIES, method = HttpMethod.PUT)
    public void updateSpecies(ChannelHandlerContext ctx, HttpRequest request, FullHttpResponse response) {
        writeJson(response.content(), db.updateSpecies(readJson(request, Species.class)));
    }

    @HttpRoute(path = URL_DB_SPECIES_ANCESTRY, method = HttpMethod.POST)
    public void speciesAncestry(ChannelHandlerContext ctx, HttpRequest request, FullHttpResponse response) {
        writeJson(response.content(), db.getSpeciesAncestry(readJson(request, Species.class)));
    }

    @HttpRoute(path = URL_DB_UPDATE_SPECIES_ANCESTRY, method = HttpMethod.PUT)
    public void updateSpeciesAncestry(ChannelHandlerContext ctx, HttpRequest request, FullHttpResponse response) {
        SpeciesAncestryData speciesAncestry = readJson(request, SpeciesAncestryData.class);

        writeJson(response.content(), db.updateSpeciesAncestry(speciesAncestry.species(), speciesAncestry.ancestry()));
    }

}
