<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Connect extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('User_model');
        $this->load->library(['form_validation', 'session']);
        $this->load->helper(['url', 'form']);
    }

    public function create() {
        $this->form_validation->set_rules('nom', 'Nom', 'required');
        $this->form_validation->set_rules('prenom', 'Prénom', 'required');
        $this->form_validation->set_rules('email', 'Adresse mail', 'valid_email|required');
        $this->form_validation->set_rules('password', 'Password', 'min_length[5]|required');
        $this->form_validation->set_rules('cpassword', 'Confirmation Password', 'required|matches[password]');
    
        if ($this->form_validation->run() === FALSE) {
            $this->load->view('layout/header');
            $this->load->view('create');
            $this->load->view('layout/footer');
        } else {
            $email = $this->input->post('email');
            if ($this->User_model->emailExists($email)) {
                $data['error'] = 'L\'adresse email est déjà utilisée.';
                $this->load->view('layout/header');
                $this->load->view('create', $data);
                $this->load->view('layout/footer');
            } else {
                $data = array(
                    'nom' => $this->input->post('nom'),
                    'prenom' => $this->input->post('prenom'),
                    'email' => $this->input->post('email'),
                    'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT)
                );
    
                if ($this->User_model->create_user($data)) {
                    redirect('connect/login');
                } else {
                    $data['error'] = 'Erreur lors de la création du compte. Veuillez réessayer.';
                    $this->load->view('layout/header');
                    $this->load->view('create', $data);
                    $this->load->view('layout/footer');
                }
            }
        }
    }
    
    

    public function login()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'required');

    if ($this->form_validation->run() === FALSE) {
        $this->load->view('layout/header');
        $this->load->view('login');
        $this->load->view('layout/footer');
    } else {
        $email = $this->input->post('email');
        $password = $this->input->post('password');

        $user = $this->User_model->get_user_by_email($email);

        if ($user && password_verify($password, $user['password'])) {
            $this->session->set_userdata([
                'user_id' => $user['id'],
                'email' => $user['email'],
                'logged_in' => TRUE
            ]);
            redirect('albums/index');
        } else {
            $data['error'] = 'Adresse email ou mot de passe incorrect';
            $this->load->view('layout/header');
            $this->load->view('login', $data);
            $this->load->view('layout/footer');
        }
    }
}


    public function logout()
    {
        $this->session->sess_destroy();
        redirect('connect/login');
    }
}