package de.viper.survivalplus.report;

import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

public class ReportManager {

    private final SurvivalPlus plugin;
    private final File reportFile;
    private FileConfiguration reportConfig;

    public ReportManager(SurvivalPlus plugin) {
        this.plugin = plugin;
        this.reportFile = new File(plugin.getDataFolder(), "reports.yml");
        if (!reportFile.exists()) {
            try {
                reportFile.createNewFile();
            } catch (IOException e) {
                plugin.getLogger().severe("Konnte reports.yml nicht erstellen!");
                e.printStackTrace();
            }
        }
        this.reportConfig = YamlConfiguration.loadConfiguration(reportFile);
    }

    public void addReport(String reportedPlayer, String reporter, String reason, String time) {
        String path = "reports." + reportedPlayer;
        List<String> reports = reportConfig.getStringList(path);
        reports.add(time + " - Von: " + reporter + " | Grund: " + reason);
        reportConfig.set(path, reports);
        saveReports();
    }

    public List<String> getReports(String reportedPlayer) {
        return reportConfig.getStringList("reports." + reportedPlayer);
    }

    public void clearReports(String reportedPlayer) {
        reportConfig.set("reports." + reportedPlayer, null);
        saveReports();
    }

    private void saveReports() {
        try {
            reportConfig.save(reportFile);
        } catch (IOException e) {
            plugin.getLogger().severe("Fehler beim Speichern der reports.yml: " + e.getMessage());
        }
    }
}
