<?php
/**
 * Plugin Name: AI Tools Directory
 * Description: A modern AI tools directory powered by WordPress content types, categories, and tags.
 * Version: 2.1.0
 * Author: Your Name
 */

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

define( 'AITD_PATH', plugin_dir_path( __FILE__ ) );
define( 'AITD_URL', plugin_dir_url( __FILE__ ) );
define( 'AITD_POST_TYPE', 'ai_tool' );
define( 'AITD_VERSION', '2.1.0' );

register_activation_hook( __FILE__, 'aitd_activate_plugin' );
function aitd_activate_plugin() {
    aitd_register_post_type();
    aitd_seed_default_categories();

    if ( function_exists( 'aitd_maybe_migrate_legacy_tools' ) ) {
        aitd_maybe_migrate_legacy_tools();
    }

    flush_rewrite_rules();
    update_option( 'aitd_plugin_version', AITD_VERSION );
}

register_deactivation_hook( __FILE__, 'aitd_deactivate_plugin' );
function aitd_deactivate_plugin() {
    flush_rewrite_rules();
}

add_action( 'init', 'aitd_maybe_flush_rewrites', 99 );
function aitd_maybe_flush_rewrites() {
    $stored_version = get_option( 'aitd_plugin_version' );

    if ( AITD_VERSION === $stored_version ) {
        return;
    }

    flush_rewrite_rules( false );
    update_option( 'aitd_plugin_version', AITD_VERSION );
}

function aitd_seed_default_categories() {
    $default_categories = [
        'Writing & Content',
        'Image & Design',
        'Video & Audio',
        'Coding & Development',
        'Productivity & Automation',
        'Marketing & SEO',
        'Research & Data',
        'Customer Support',
    ];

    foreach ( $default_categories as $category_name ) {
        if ( ! term_exists( $category_name, 'category' ) ) {
            wp_insert_term( $category_name, 'category' );
        }
    }
}

add_action( 'init', 'aitd_register_post_type', 5 );
function aitd_register_post_type() {
    $labels = [
        'name'               => __( 'AI Tools', 'aitd' ),
        'singular_name'      => __( 'AI Tool', 'aitd' ),
        'add_new'            => __( 'Add New', 'aitd' ),
        'add_new_item'       => __( 'Add New AI Tool', 'aitd' ),
        'edit_item'          => __( 'Edit AI Tool', 'aitd' ),
        'new_item'           => __( 'New AI Tool', 'aitd' ),
        'view_item'          => __( 'View AI Tool', 'aitd' ),
        'search_items'       => __( 'Search AI Tools', 'aitd' ),
        'not_found'          => __( 'No AI tools found.', 'aitd' ),
        'not_found_in_trash' => __( 'No AI tools found in Trash.', 'aitd' ),
        'menu_name'          => __( 'AI Tools', 'aitd' ),
    ];

    register_post_type(
        AITD_POST_TYPE,
        [
            'labels'             => $labels,
            'public'             => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'menu_icon'          => 'dashicons-superhero',
            'supports'           => [ 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields' ],
            'has_archive'        => 'ai-tools',
            'rewrite'            => [
                'slug'       => 'ai-tools',
                'with_front' => false,
            ],
            'show_in_rest'       => true,
            'taxonomies'         => [ 'category', 'post_tag' ],
            'menu_position'      => 30,
            'publicly_queryable' => true,
        ]
    );
}

require_once AITD_PATH . 'includes/admin.php';
require_once AITD_PATH . 'includes/frontend.php';
require_once AITD_PATH . 'includes/shortcodes.php';
