const Controller = require("./Controller");
const { login, logout } = require("../service/authService");
module.exports = new class AuthController extends Controller {
    admin = async (req, res, next) => {
        try {
            const { email, password } = req.body;
            if (!email || !password) return next("Input field empty");
            const token = await login({ email, password }, "admins");
            return res.success("success", { token })
        } catch (error) {
            return next(error.toString());
        }
    };
    // user auhintication
    user = async (req, res, next) => {
        try {
            const { name, password } = req.body;
            if (!name || !password) return next("Input value empty!");
            const token = await login({ name, "pass": password }, "users");
            return res.success("success", {
                "token": token
            });
        } catch (error) {
            console.log(error);
            return next(error.toString());
        }
    }
    sinup = async (req, res, next) => {
        try {
            let { name, mobile, club, sponser = null, password } = req.body;
            // basic presence check
            if (!name || !mobile || !club || !password) return next("Input value empty");
            // value constraints
            if (name.length < 3) return next("Name must be at least 3 characters");
            const mobileRegex = /^[0-9+\- ]{7,15}$/;
            if (!mobileRegex.test(mobile)) return next("Invalid mobile number");
            // club must be a valid id and exist
            const clubId = parseInt(club.toString());
            if (isNaN(clubId)) return next("Invalid club id");
            const clubRecord = await this.database.table(this.table).select("*").where("type", "=", this.useType.club).where("id", "=", clubId);
            if (!clubRecord || clubRecord.length === 0) return next("Invalid club id");
            // sponsor check (optional)
            let sid = 0;
            if (sponser) {
                const sp = await this.database.table(this.table).select("*").where("type", "=", this.useType.user).where("name", "=", sponser);
                if (!sp || sp.length === 0) {
                    return next("Invalid sponsor username");
                }
                sid = sp[0].id;
            }
            // password constraints
            if (typeof password !== "string" || password.length < 6) {
                return next("Minimum password length is 8 characters");
            }

            // uniqueness checks
            const checkName = await this.database.table(this.table).select("*").where("name", "=", name);
            if (checkName.length > 0) {
                return next("Username already exists");
            }
            const checkMobile = await this.database.table(this.table).select("*").where("mobail", "=", mobile);
            if (checkMobile.length > 0) {
                return next("Mobile number already registered");
            }
            // create pass hash
            const hashedPassword = await bcrypt.hash(password, 10);
            // insert user into database
            const result = await this.database.table(this.table).insert({
                name,
                mobail: mobile,
                club_by_id: clubId,
                sponcer_by_id: sid,
                pass: hashedPassword,
                type: this.useType.user
            });

            if (result) {
                return res.success("Add successful");
            }
            return next("Some error occurred");
        } catch (error) {
            return next(error.toString());
        }
    }
    logout = async (req, res, next) => {
        try {
            await logout(req.token);
            return res.success("successfull");
        } catch (error) {
            return next(error.toString())
        }
    }
    genaretpass = () => {
        
    }
};