import express from "express";

import Authentication from "./routes/authentication"
import TicketCreation from "./routes/ticket_creation"
import TicketEditing from "./routes/ticket_editing"

import TicketLists from "./routes/ticket_lists"
import TicketStatus from "./routes/ticket_status"

import mysql2 from "mysql2/promise"

import cors from "cors"

export const seed = "skills2025" // These should be in the environment.. the seed is used for hashing the password
export const connection = await mysql2.createConnection({
    host: "cp1.skills2024.dev", 
    port: 3306,
    user: "rpzmh2qw_user",
    password: "XNZ.o9lrbrvv",
    database: "rpzmh2qw_db"
});

//#region Create default users
const userQuery = await connection.query("SELECT * FROM users WHERE name = 'User1';");
if((userQuery[0] as any[]).length == 0) {
    [
        ["User1", "password1"],
        ["User2", "password2"],
        ["Admin1", "adminpassword1"],
        ["Admin2", "adminpassword2"],
    ].forEach(async z => {
        await connection.query("INSERT INTO `users` (`id`, `name`, `password`, `admin`, `token`) VALUES (NULL, ?, ?, '1', ?);", [
            z[0], await Bun.password.hash(seed + z[1]), crypto.randomUUID().replaceAll("-", "")+crypto.randomUUID().replaceAll("-", "")
        ])
    })
}
//#endregion Create default users

const app = express();

app.use(cors());
app.use("/auth", Authentication)
app.use("/ticket", TicketCreation)
app.use("/ticket", TicketEditing)
app.use("/ticket", TicketLists)
app.use("/ticket", TicketStatus)

app.get("/", (req, res) => {
    res.send("SVEIKI SKILLSLATVIA2025!!!!!!!!!!!!!!!!!!!!!!!!!")
    res.end();
})

app.listen(3000, "0.0.0.0", () => {
    console.log("port 3k running")
})