package com.developx.szpitale.load;

import org.neo4j.driver.Driver;
import org.neo4j.driver.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GraphSchema {

    private static final Logger log = LoggerFactory.getLogger(GraphSchema.class);

    private final Driver driver;

    public GraphSchema(Driver driver) {
        this.driver = driver;
    }

    public void createConstraints() {
        try (Session session = driver.session()) {
            session.run("CREATE CONSTRAINT hospital_id IF NOT EXISTS FOR (h:Hospital) REQUIRE h.id IS UNIQUE");
            session.run("CREATE CONSTRAINT person_id IF NOT EXISTS FOR (p:Person) REQUIRE p.id IS UNIQUE");
            session.run("CREATE CONSTRAINT party_name IF NOT EXISTS FOR (x:Party) REQUIRE x.name IS UNIQUE");
            session.run("CREATE CONSTRAINT voiv_slug IF NOT EXISTS FOR (v:Voivodeship) REQUIRE v.slug IS UNIQUE");
            session.run("CREATE INDEX person_name IF NOT EXISTS FOR (p:Person) ON (p.fullName)");
            session.run("CREATE INDEX govbody_name IF NOT EXISTS FOR (g:GovBody) ON (g.name)");
            log.info("Graph constraints and indexes created");
        }
    }

    public void dropAll() {
        try (Session session = driver.session()) {
            session.run("MATCH (n) DETACH DELETE n");
            log.info("All nodes and relationships deleted");
        }
    }
}
