// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import { OkPacket, QueryError, RowDataPacket } from "mysql2";
import type { NextApiRequest, NextApiResponse } from 'next';
import { pool } from "../../../server/db/connect";
import { HashPassword } from '../../../components/admin/function/hashPassword'

type Data = {
    result: string;
    anObject:  string;
}
const sql = ["SELECT salt FROM user WHERE login=?",
             "SELECT salt, id FROM user WHERE password_hash=?",
             "UPDATE user SET token = ? WHERE id = ?"];

export default function handler(
    req: NextApiRequest,
    res: NextApiResponse<Data>)
    {
        pool.query(
            sql[0],
            ['admin'],
            (err, result:RowDataPacket[0]) => {
              if (err) {console.log(err)};
              result.length ?
                console.log(result[0]) :
                res.status(200).json({ {error:'dsd'}});
            }
          );
        /*pool.query(sql[0], [req.login], (err: QueryError, rows: RowDataPacket[0]) => {
            if (rows.length<1) {
                res.status(200).json({error:'Неверный логин'});
                console.log({error:'Неверный логин'});
            } else {
              /*  hashPassword(_data.password, rows[0].salt, function(hash) {
                    pool.query(sql[1], [hash], function(err, rows2) {
                        if (rows2.length == 1) {
                            hashPassword(_data.login+_data.password, rows2[0].salt , function(token) {
                                pool.query(sql[2] [token, rows2[0].id], function() {
                                    res.status(200).json({'token':token});
                                 })
                            });
                        } else {
                            res.status(200).json({error:'Неверный пароль'});
                            console.log({error:'Неверный пароль'});
                        }
                     })
                }); 
            }
         })*/
        
    };