<?php

class MauticEducationExperience extends Core
{

    function store()
    {

        $educations = json_decode($_POST['educations'], true);
        $email = $_POST['email'];

        $sql = "SELECT id FROM leads WHERE email = ?";
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute([$email]);
        $lead = $stmt->fetch(PDO::FETCH_ASSOC);

        $sql = "INSERT INTO education_experience(lead_id, education_level, institution_name, course_name, date_start, date_end)
                VALUES (?, ?, ?, ?, ?, ?)";
        $stmt = parent::$conn->prepare($sql);

        foreach ($educations as $education) {

            $stmt->execute([
                $lead['id'],
                $education['educationLevel'],
                $education['institutionName'],
                $education['courseName'],
                $education['dateStart'],
                $education['currentCourse'] == 'true' ? null : $education['dateEnd']
            ]);
        }

        return ['type' => 'OK'];
    }

    function update()
    {

        $educations = json_decode($_POST['educations'], true);
        $email = $_POST['email'];

        $sql = "SELECT id FROM leads WHERE email = ?";
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute([$email]);
        $lead = $stmt->fetch(PDO::FETCH_ASSOC);

        $sql = "UPDATE education_experience SET education_level = :education_level,  
            institution_name = :institution_name, course_name = :course_name, 
            date_start = :date_start, date_end = :date_end 
            WHERE id = :id AND lead_id = :lead_id";
        $stmt = parent::$conn->prepare($sql);

        foreach ($educations as $education) {

            $stmt->execute([
                ':id' => $education['id'],
                ':lead_id' => $lead['id'],
                ':education_level' => $education['educationLevel'],
                ':institution_name' => $education['institutionName'],
                ':course_name' => $education['courseName'],
                ':date_start' => $education['dateStart'],
                ':date_end' => $education['currentCourse'] == 'true' ? null : $education['dateEnd']
            ]);
        }

        return ['type' => 'OK'];
    }

    function list()
    {
        $email = $_GET['email'];
        $sql = "SELECT id FROM leads WHERE email = ?";
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute([$email]);
        $lead = $stmt->fetch(PDO::FETCH_ASSOC);

        $sql = "SELECT * FROM education_experience WHERE lead_id = :id";
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute([':id' => $lead['id']]);
        $experiences = $stmt->fetchAll(PDO::FETCH_ASSOC);

        return ['type' => 'OK', 'result' => $experiences];
    }

    function delete()
    {
        $id = $_GET['id'];
        $email = $_GET['email'];
        $sql = "SELECT id FROM leads WHERE email = ?";
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute([$email]);
        $lead = $stmt->fetch(PDO::FETCH_ASSOC);

        $sql = "DELETE FROM education_experience WHERE lead_id = :lead_id AND id = :id";
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute([
            ':id' => $id,
            ':lead_id' => $lead['id']
        ]);

        return ['type' => 'OK'];
    }
}
