<?php
/**
 * Custom post types: service (服务项目/膜品) and case (案例作品集).
 * 主题激活时把 Customizer 中的 7 个产品迁移为 service 文章，并创建默认页面。
 *
 * @package Fragrance_Trade
 */

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

/** Register the service and case post types plus the case_type taxonomy. */
function fragrance_trade_register_post_types() {
	register_post_type(
		'service',
		array(
			'labels'            => array(
				'name'          => __( '服务项目', 'fragrance-trade' ),
				'singular_name' => __( '服务项目', 'fragrance-trade' ),
				'add_new'       => __( '添加服务项目', 'fragrance-trade' ),
				'edit_item'     => __( '编辑服务项目', 'fragrance-trade' ),
				'all_items'     => __( '全部服务项目', 'fragrance-trade' ),
			),
			'public'            => true,
			'has_archive'       => 'services',
			'rewrite'           => array( 'slug' => 'services' ),
			'menu_position'     => 20,
			'menu_icon'         => 'dashicons-admin-customizer',
			'supports'          => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
			'show_in_rest'      => true,
		)
	);

	register_post_type(
		'case',
		array(
			'labels'            => array(
				'name'          => __( '案例作品', 'fragrance-trade' ),
				'singular_name' => __( '案例', 'fragrance-trade' ),
				'add_new'       => __( '添加案例', 'fragrance-trade' ),
				'edit_item'     => __( '编辑案例', 'fragrance-trade' ),
				'all_items'     => __( '全部案例', 'fragrance-trade' ),
			),
			'public'            => true,
			'has_archive'       => 'cases',
			'rewrite'           => array( 'slug' => 'cases' ),
			'menu_position'     => 21,
			'menu_icon'         => 'dashicons-format-gallery',
			'supports'          => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
			'show_in_rest'      => true,
		)
	);

	register_taxonomy(
		'case_type',
		'case',
		array(
			'labels'            => array(
				'name'          => __( '案例分类', 'fragrance-trade' ),
				'singular_name' => __( '案例分类', 'fragrance-trade' ),
			),
			'public'            => true,
			'hierarchical'      => false,
			'show_in_rest'      => true,
			'rewrite'           => array( 'slug' => 'case-type' ),
		)
	);
}
add_action( 'init', 'fragrance_trade_register_post_types' );

/** Filter the case archive by the case_type query argument. */
function fragrance_trade_filter_cases_archive( $query ) {
	if ( is_admin() || ! $query->is_main_query() ) {
		return;
	}
	if ( $query->is_post_type_archive( 'case' ) && ! empty( $_GET['case_type'] ) ) {
		$query->set( 'case_type', sanitize_text_field( wp_unslash( $_GET['case_type'] ) ) );
	}
}
add_action( 'pre_get_posts', 'fragrance_trade_filter_cases_archive' );

/** One-time setup on theme activation: migrate products and create pages. */
function fragrance_trade_setup_site_on_activation() {
	if ( get_option( 'fragrance_trade_setup_done' ) ) {
		return;
	}

	$product_fallbacks = array(
		'products/placeholder-01.svg',
		'products/placeholder-02.svg',
		'products/placeholder-03.svg',
		'products/placeholder-04.svg',
		'products/placeholder-05.svg',
		'products/placeholder-06.svg',
		'products/placeholder-07.svg',
	);
	for ( $index = 1; $index <= count( $product_fallbacks ); $index++ ) {
		$title = fragrance_trade_get_mod( 'b2b_product_' . $index . '_title' );
		if ( ! $title ) {
			continue;
		}
		$post_id = wp_insert_post(
			array(
				'post_type'    => 'service',
				'post_title'   => $title,
				'post_content' => fragrance_trade_get_mod( 'b2b_product_' . $index . '_text' ),
				'post_excerpt' => fragrance_trade_get_mod( 'b2b_product_' . $index . '_specs' ),
				'post_status'  => 'publish',
			)
		);
		if ( $post_id && ! is_wp_error( $post_id ) ) {
			$image = fragrance_trade_get_mod( 'b2b_product_' . $index . '_image', '' );
			if ( $image ) {
				update_post_meta( $post_id, '_service_image', $image );
			}
		}
	}

	$pages = array(
		'home'    => array( 'post_title' => __( '首页', 'fragrance-trade' ), 'template' => '' ),
		'about'   => array( 'post_title' => __( '关于门店', 'fragrance-trade' ), 'template' => 'page-about.php' ),
		'faq'     => array( 'post_title' => __( '常见问题', 'fragrance-trade' ), 'template' => 'page-faq.php' ),
		'contact' => array( 'post_title' => __( '预约/联系', 'fragrance-trade' ), 'template' => 'page-contact.php' ),
	);
	$created = array();
	foreach ( $pages as $slug => $data ) {
		$existing = get_page_by_path( $slug );
		if ( $existing ) {
			$created[ $slug ] = $existing->ID;
			continue;
		}
		$id = wp_insert_post(
			array(
				'post_type'     => 'page',
				'post_name'     => $slug,
				'post_title'    => $data['post_title'],
				'post_status'   => 'publish',
				'page_template' => $data['template'],
			)
		);
		if ( $id && ! is_wp_error( $id ) ) {
			$created[ $slug ] = $id;
		}
	}

	if ( ! empty( $created['home'] ) ) {
		update_option( 'page_on_front', $created['home'] );
		update_option( 'show_on_front', 'page' );
	}

	flush_rewrite_rules();
	update_option( 'fragrance_trade_setup_done', 1 );
}
add_action( 'after_switch_theme', 'fragrance_trade_setup_site_on_activation' );
