<?php
if (!defined('ABSPATH')) {
    exit;
}

class BMTQB_Submission_Post_Type {
    private $industries = [];

    public function register(BMTQB_Industry_Interface $industry) {
        $this->industries[$industry->key()] = $industry;

        add_action('init', function () use ($industry) {
            $this->register_post_type($industry);
        });

        add_action('admin_menu', function () use ($industry) {
            $this->register_submissions_menu($industry);
        }, 30);
    }

    private function register_post_type(BMTQB_Industry_Interface $industry) {
        $plural = $industry->label() . 's';

        $labels = [
            'name' => $plural,
            'singular_name' => $industry->label(),
            'menu_name' => 'Quote Submissions',
            'add_new' => 'Add Submission',
            'add_new_item' => 'Add Submission',
            'edit_item' => 'Edit Submission',
            'new_item' => 'New Submission',
            'view_item' => 'View Submission',
            'search_items' => 'Search Submissions',
            'not_found' => 'No submissions found',
            'not_found_in_trash' => 'No submissions found in Trash',
        ];

        register_post_type($industry->post_type(), [
            'labels' => $labels,
            'public' => false,
            'show_ui' => true,
            'show_in_menu' => false,
            'supports' => ['title'],
            'menu_icon' => 'dashicons-media-spreadsheet',
            'capability_type' => ['quote_submission', 'quote_submissions'],
            'map_meta_cap' => true,
            'capabilities' => [
                'read' => BMTQB_Capabilities::VIEW_SUBMISSIONS,
                'edit_post' => BMTQB_Capabilities::EDIT_SUBMISSIONS,
                'read_post' => BMTQB_Capabilities::VIEW_SUBMISSIONS,
                'delete_post' => BMTQB_Capabilities::DELETE_SUBMISSIONS,
                'edit_posts' => BMTQB_Capabilities::EDIT_SUBMISSIONS,
                'edit_others_posts' => BMTQB_Capabilities::EDIT_SUBMISSIONS,
                'create_posts' => BMTQB_Capabilities::EDIT_SUBMISSIONS,
                'publish_posts' => BMTQB_Capabilities::EDIT_SUBMISSIONS,
                'read_private_posts' => BMTQB_Capabilities::VIEW_SUBMISSIONS,
                'delete_posts' => BMTQB_Capabilities::DELETE_SUBMISSIONS,
                'delete_private_posts' => BMTQB_Capabilities::DELETE_SUBMISSIONS,
                'delete_published_posts' => BMTQB_Capabilities::DELETE_SUBMISSIONS,
                'delete_others_posts' => BMTQB_Capabilities::DELETE_SUBMISSIONS,
            ],
        ]);
    }
    private function register_submissions_menu(BMTQB_Industry_Interface $industry) {
        add_submenu_page(
            $industry->settings_menu_slug(),
            'Submissions',
            'Submissions',
            BMTQB_Capabilities::EDIT_SUBMISSIONS,
            'edit.php?post_type=' . $industry->post_type()
        );
    }
}
