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

/**
 * Contact — contact page and inquiry submission.
 */
#[\AllowDynamicProperties]
class Contact extends Public_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model(array('inquiry_model'));
	}

	/** Contact page. */
	public function index()
	{
		$this->meta(array(
			'title'       => 'Contact — Bloom Iris Agency',
			'description' => 'Get in touch with Bloom Iris Agency. Let\'s create something unforgettable.',
		));
		$this->render('contact/index', array(
			'services' => $this->service_model->active(),
		));
	}

	/** Handle contact-form submission. */
/** Handle contact-form submission. */
public function submit()
{
    // Enable logging
    log_message('debug', 'Contact form submission started');
    
    $this->load->library('form_validation');
    $this->form_validation->set_rules('name', 'Name', 'required|trim|max_length[140]');
    $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
    $this->form_validation->set_rules('subject', 'Subject', 'trim|max_length[200]');
    $this->form_validation->set_rules('message', 'Message', 'required|trim');

    if ($this->form_validation->run() === FALSE)
    {
        $error = strip_tags(validation_errors()) ?: 'Please complete all required fields.';
        log_message('error', 'Contact form validation failed: ' . $error);
        $this->session->set_flashdata('error', $error);
        redirect('contact');
    }

    // Honeypot — disable for now as it's catching legitimate submissions
    // TODO: Implement a better honeypot solution
    /*
    if ($this->input->post('website'))
    {
        log_message('debug', 'Honeypot triggered - bot detected');
        redirect('contact');
    }
    */

    // Prepare data for insertion
    $insert_data = array(
        'type'       => 'contact',
        'name'       => $this->input->post('name', TRUE),
        'email'      => $this->input->post('email', TRUE),
        'phone'      => $this->input->post('phone', TRUE),
        'subject'    => $this->input->post('subject', TRUE),
        'message'    => $this->input->post('message', TRUE),
        'status'     => 'new',
        'ip_address' => $this->input->ip_address(),
        'created_at' => date('Y-m-d H:i:s')
    );
    
    log_message('debug', 'Attempting to insert inquiry: ' . print_r($insert_data, true));
    
    // Insert into database
    $id = $this->inquiry_model->insert($insert_data);
    
    if (!$id) {
        log_message('error', 'Failed to insert inquiry into database');
        $this->session->set_flashdata('error', 'There was a technical issue. Please try again or call us directly.');
        redirect('contact');
    }
    
    log_message('debug', 'Inquiry inserted successfully with ID: ' . $id);

    // Load mailer and send notifications
    $this->load->library('mailer');
    
    // Notify admin (optional - don't break if fails)
    $this->mailer->notify_admin(
        'New Contact Inquiry — ' . ($this->input->post('subject', TRUE) ?: 'No Subject'),
        'New Contact Inquiry',
        '<p><strong>' . html_escape($this->input->post('name', TRUE)) . '</strong> sent a message:</p>'
        . '<p>' . nl2br(html_escape($this->input->post('message', TRUE))) . '</p>'
        . '<p><strong>Contact Details:</strong><br>'
        . 'Email: ' . html_escape($this->input->post('email', TRUE)) . '<br>'
        . 'Phone: ' . html_escape($this->input->post('phone', TRUE)) . '</p>'
    );
    
    // Send auto-response to user (optional)
    $this->mailer->send(
        $this->input->post('email', TRUE),
        'We received your message — Bloom Iris Agency',
        'Thank you for reaching out',
        '<p>Hi ' . html_escape($this->input->post('name', TRUE)) . ',</p>'
        . '<p>Thank you for contacting Bloom Iris Agency. We\'ve received your message '
        . 'and a member of our team will be in touch very soon.</p>'
        . '<p><strong>Your message:</strong></p>'
        . '<p style="background:#f5f5f5;padding:15px;border-radius:8px;">' 
        . nl2br(html_escape($this->input->post('message', TRUE))) . '</p>'
        . '<p>Warm regards,<br>The Bloom Iris Team</p>'
    );

    $this->session->set_flashdata('success', 'Thank you! Your message has been sent — we\'ll be in touch soon.');
    redirect('contact');
}

}