import pool from '../../../../../../lib/db/db';
import { NextRequest, NextResponse } from 'next/server';
import { ResultSetHeader, RowDataPacket } from 'mysql2';
import bcrypt from 'bcryptjs';

export async function POST(req: NextRequest) {
  const connection = await pool.getConnection(); // ✅ get connection
  try {
    const body = await req.json();


    const {
        company_name,
        designation,
        number_of_employees,
        address,
        first_name,
        last_name,
        mobile_number,
        comment,
        agree,
        email,
        password,
        nationality,
        state,
        city,
    } = body;

    // email check (outside transaction is fine, but safer inside)
    const [checkEmail] = await connection.query<RowDataPacket[]>(
      "SELECT * FROM users WHERE email = ? LIMIT 1",
      [email]
    );

    if (checkEmail.length > 0) {
      connection.release();
      return NextResponse.json(
        {
          success: false,
          message: "This email is already registered. Please choose another one"
        },
        { status: 400 }
      );
    }

    // ✅ start transaction
    await connection.beginTransaction();

    const hashedPassword = await bcrypt.hash(password, 10);

    const [results] = await connection.query<ResultSetHeader>(
      "INSERT INTO users (first_name, last_name, email, password, user_type) VALUES (?, ?, ?, ?, ?)",
      [first_name, last_name, email, hashedPassword, "consultant"]
    );

    if (results.affectedRows === 0) {
      throw new Error("Failed to insert user");
    }

    const full_name = `${first_name} ${last_name}`;

    

    const [studentData] = await connection.query<ResultSetHeader>(
      "INSERT INTO consultants (user_id, name, company_name, nationality, state, city, address, designation, email, phone, comment, employeeno) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
      [results.insertId, full_name, company_name, nationality, state, city, address, designation, email, mobile_number, comment, number_of_employees]
    );

    const consultantData = {
      name: full_name,
      company_name: company_name,
      nationality: nationality,
      state: state,
      city: city,
      address: address,
      designation: designation,
      email: email,
      phone: mobile_number,
      comment: comment,
      employeeno: number_of_employees,
      consultant_id: results.insertId
    };

    if (studentData.affectedRows === 0) {
      throw new Error("Failed to insert Consultant");
    }

    console.log(JSON.stringify(consultantData));

    const response = await fetch("https://crm-universitiespage.com/reactapis/api/website_consultant_crm/store", {
      method: "POST",
      body: JSON.stringify(consultantData),
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `${process.env.CRM_API_KEY}`,
      },
    })

    const data = await response.json();

    if (data?.status_code !== 200) {

      await connection.rollback();

      return NextResponse.json(
        {
          message: "Failed to submit your form. Please try again later.",
          success: false
        },
        {
          status: 500
        }
      )
    }

    // ✅ commit transaction
    await connection.commit();

    console.log("Data Inserted successfully.")

    return NextResponse.json(
      { success: true, message: "Consultant Registered Successfully" },
      { status: 200 }
    );
  } catch (err: any) {
    // ❌ rollback transaction if active
    try {
      await connection.rollback();
    } catch (rollbackErr) {
      console.error("Rollback failed:", rollbackErr);
    }

    if (err?.issues) {
      // ZodError
      const fieldErrors: Record<string, string[]> = {};
      for (const issue of err.issues) {
        const key = (issue.path?.[0] as string) ?? "_";
        fieldErrors[key] = fieldErrors[key] || [];
        fieldErrors[key].push(issue.message);
      }
      return NextResponse.json(
        { ok: false, message: fieldErrors },
        { status: 400 }
      );
    }

    console.error("POST /register error:", err);
    return NextResponse.json(
      { ok: false, message: "Internal server error" },
      { status: 500 }
    );
  } finally {
    connection.release(); // ✅ always release
  }
}
