<?php
/**
 * Contact inquiries — stores every contact-form submission as a private
 * CPT so nothing is ever lost, even if the outgoing email fails or is
 * spam-filtered. Visible to the site owner under "Inquiries" in wp-admin.
 *
 * @package    Odysseo
 * @author     Chandrabhan Shekhawat
 * @company    Gigai Kripa Services
 * @link       https://gigaikripaservices.com/
 * @copyright  Copyright (c) 2026, Gigai Kripa Services
 */

if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Register the (admin-only) inquiry post type.
 */
function odysseo_register_inquiry_cpt() {
	register_post_type( 'odysseo_inquiry', array(
		'labels' => array(
			'name'          => __( 'Inquiries', 'odysseo-core' ),
			'singular_name' => __( 'Inquiry', 'odysseo-core' ),
			'menu_name'     => __( 'Inquiries', 'odysseo-core' ),
			'all_items'     => __( 'All Inquiries', 'odysseo-core' ),
			'edit_item'     => __( 'View Inquiry', 'odysseo-core' ),
			'search_items'  => __( 'Search Inquiries', 'odysseo-core' ),
			'not_found'     => __( 'No inquiries yet.', 'odysseo-core' ),
		),
		'public'              => false,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'show_in_nav_menus'   => false,
		'exclude_from_search' => true,
		'publicly_queryable'  => false,
		'has_archive'         => false,
		'rewrite'             => false,
		'menu_icon'           => 'dashicons-email-alt',
		'menu_position'       => 26,
		'capability_type'     => 'post',
		'capabilities'        => array(
			'create_posts' => 'do_not_allow', // Submissions are created programmatically only.
		),
		'map_meta_cap'        => true,
		'supports'            => array( 'title', 'editor' ),
	) );
}
add_action( 'init', 'odysseo_register_inquiry_cpt' );

/**
 * Persist one inquiry. Returns the new post ID or 0 on failure.
 *
 * @param array $data Keys: name, email, phone, subject, message.
 * @return int
 */
function odysseo_store_inquiry( $data ) {
	$name    = isset( $data['name'] ) ? $data['name'] : '';
	$subject = isset( $data['subject'] ) ? $data['subject'] : '';

	$title = trim( ( $subject ? $subject : __( 'Inquiry', 'odysseo-core' ) ) . ' — ' . $name );

	$post_id = wp_insert_post( array(
		'post_type'    => 'odysseo_inquiry',
		'post_status'  => 'private',
		'post_title'   => $title,
		'post_content' => isset( $data['message'] ) ? $data['message'] : '',
	), true );

	if ( ! $post_id || is_wp_error( $post_id ) ) {
		return 0;
	}

	update_post_meta( $post_id, '_inq_name',    $name );
	update_post_meta( $post_id, '_inq_email',   isset( $data['email'] ) ? $data['email'] : '' );
	update_post_meta( $post_id, '_inq_phone',   isset( $data['phone'] ) ? $data['phone'] : '' );
	update_post_meta( $post_id, '_inq_subject', $subject );

	return $post_id;
}

/**
 * Admin list columns for inquiries.
 */
function odysseo_inquiry_columns( $columns ) {
	return array(
		'cb'           => isset( $columns['cb'] ) ? $columns['cb'] : '',
		'title'        => __( 'Subject', 'odysseo-core' ),
		'inq_name'     => __( 'Name', 'odysseo-core' ),
		'inq_email'    => __( 'Email', 'odysseo-core' ),
		'inq_phone'    => __( 'Phone', 'odysseo-core' ),
		'date'         => __( 'Received', 'odysseo-core' ),
	);
}
add_filter( 'manage_odysseo_inquiry_posts_columns', 'odysseo_inquiry_columns' );

/**
 * Render the custom inquiry columns.
 */
function odysseo_inquiry_column_content( $column, $post_id ) {
	if ( 'inq_name' === $column ) {
		echo esc_html( get_post_meta( $post_id, '_inq_name', true ) );
	} elseif ( 'inq_email' === $column ) {
		$email = get_post_meta( $post_id, '_inq_email', true );
		if ( $email ) {
			echo '<a href="' . esc_url( 'mailto:' . $email ) . '">' . esc_html( $email ) . '</a>';
		}
	} elseif ( 'inq_phone' === $column ) {
		echo esc_html( get_post_meta( $post_id, '_inq_phone', true ) );
	}
}
add_action( 'manage_odysseo_inquiry_posts_custom_column', 'odysseo_inquiry_column_content', 10, 2 );
