<?php
/**
 * Capability slugs and the role-to-capability matrix.
 *
 * Single source of truth for every wrenchpress_* capability and which role
 * holds it. The activator, admin pages, and uninstall routine all reference
 * the constants and matrix defined here so the strings never drift.
 *
 * @package WrenchPress
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class WrenchPress_Capabilities
 */
class WrenchPress_Capabilities {

	/**
	 * View (read) customer records. Held by every staff role; the mechanic
	 * menu is still hidden in Phase 1 (D-04) but the capability exists for
	 * Phase 3 when work orders give mechanics a reason to look up a customer.
	 */
	const VIEW_CUSTOMERS = 'wrenchpress_view_customers';

	/**
	 * Create customer records.
	 */
	const CREATE_CUSTOMERS = 'wrenchpress_create_customers';

	/**
	 * Edit customer records.
	 */
	const EDIT_CUSTOMERS = 'wrenchpress_edit_customers';

	/**
	 * Delete (soft-delete / archive) customer records.
	 */
	const DELETE_CUSTOMERS = 'wrenchpress_delete_customers';

	/**
	 * Add work-order progress notes. Reserved now (D-05) so it slots into the
	 * mechanic role in Phase 3 without reworking the existing role definitions.
	 * Granted to no role in Phase 1.
	 */
	const ADD_WORKORDER_NOTES = 'wrenchpress_add_workorder_notes';

	/**
	 * View (read) vehicle records. Held by every staff role, including the
	 * mechanic, who needs to look up the vehicle attached to a job.
	 */
	const VIEW_VEHICLES = 'wrenchpress_view_vehicles';

	/**
	 * Create vehicle records. Owner/manager and front desk only; the mechanic
	 * works the job, not the customer/vehicle book (D-20).
	 */
	const CREATE_VEHICLES = 'wrenchpress_create_vehicles';

	/**
	 * Edit vehicle records. Owner/manager and front desk only.
	 */
	const EDIT_VEHICLES = 'wrenchpress_edit_vehicles';

	/**
	 * Delete (archive) vehicle records. Owner/manager and front desk only.
	 */
	const DELETE_VEHICLES = 'wrenchpress_delete_vehicles';

	/**
	 * Manage work orders: create and delete work orders and edit their priced
	 * line items. Held by owner/manager and front desk only; the mechanic does
	 * NOT hold this because pricing and job creation are front-desk work (D-20).
	 */
	const MANAGE_WORKORDERS = 'wrenchpress_manage_workorders';

	/**
	 * Change a work order's status. Held by owner/manager, front desk, and the
	 * mechanic, who moves a job through its stages without touching pricing.
	 */
	const CHANGE_WORKORDER_STATUS = 'wrenchpress_change_workorder_status';

	/**
	 * Manage incoming leads: view the Leads worklist and act on a lead (dismiss,
	 * mark spam, promote). Held by owner/manager and front desk; mechanics do not
	 * triage incoming visit requests so they are excluded.
	 */
	const MANAGE_LEADS = 'wrenchpress_manage_leads';

	/**
	 * Manage appointments: create, edit, and cancel appointments on the calendar.
	 * Held by owner/manager and front desk; the mechanic works the job, not the
	 * schedule, so it is withheld the same way MANAGE_WORKORDERS is.
	 */
	const MANAGE_APPOINTMENTS = 'wrenchpress_manage_appointments';

	/**
	 * View the appointment calendar. Held by every staff role, including the
	 * mechanic, who needs to see the day's schedule without editing it.
	 */
	const VIEW_APPOINTMENTS = 'wrenchpress_view_appointments';

	/**
	 * Manage service reminders: work the follow-up worklist and mark reminders
	 * handled or dismissed. Held by owner/manager and front desk; the mechanic
	 * works the job, not the follow-up, so it is withheld the same way
	 * MANAGE_APPOINTMENTS is.
	 */
	const MANAGE_REMINDERS = 'wrenchpress_manage_reminders';

	/**
	 * Role slug: Owner / Manager.
	 */
	const ROLE_OWNER = 'wrenchpress_owner';

	/**
	 * Role slug: Front Desk.
	 */
	const ROLE_FRONT_DESK = 'wrenchpress_front_desk';

	/**
	 * Role slug: Mechanic.
	 */
	const ROLE_MECHANIC = 'wrenchpress_mechanic';

	/**
	 * Every capability this plugin defines.
	 *
	 * @return string[] List of capability slugs.
	 */
	public static function all_capabilities() {
		return array(
			self::VIEW_CUSTOMERS,
			self::CREATE_CUSTOMERS,
			self::EDIT_CUSTOMERS,
			self::DELETE_CUSTOMERS,
			self::ADD_WORKORDER_NOTES,
			self::VIEW_VEHICLES,
			self::CREATE_VEHICLES,
			self::EDIT_VEHICLES,
			self::DELETE_VEHICLES,
			self::MANAGE_WORKORDERS,
			self::CHANGE_WORKORDER_STATUS,
			self::MANAGE_LEADS,
			self::MANAGE_APPOINTMENTS,
			self::VIEW_APPOINTMENTS,
			self::MANAGE_REMINDERS,
		);
	}

	/**
	 * Role-to-capability matrix.
	 *
	 * Each entry maps a role slug to its display name and the capabilities it
	 * holds (as a cap => true map suitable for add_role()). The wrenchpress
	 * text domain is intentionally NOT applied to display names here because
	 * this runs at activation, before init; WordPress stores the raw name and
	 * the Users screen renders it. Translating role display names at activation
	 * would fire __() too early and trigger the JIT loader notice.
	 *
	 * @return array<string, array{display_name: string, capabilities: array<string, bool>}>
	 */
	public static function role_matrix() {
		return array(
			self::ROLE_OWNER      => array(
				'display_name' => 'PitDesk Owner/Manager',
				'capabilities' => array(
					// Core read is required for wp-admin access; without it
					// WordPress blocks the dashboard for the user entirely.
					'read'                        => true,
					self::VIEW_CUSTOMERS          => true,
					self::CREATE_CUSTOMERS        => true,
					self::EDIT_CUSTOMERS          => true,
					self::DELETE_CUSTOMERS        => true,
					self::MANAGE_LEADS            => true,
					self::VIEW_VEHICLES           => true,
					self::CREATE_VEHICLES         => true,
					self::EDIT_VEHICLES           => true,
					self::DELETE_VEHICLES         => true,
					self::MANAGE_WORKORDERS       => true,
					self::CHANGE_WORKORDER_STATUS => true,
					self::ADD_WORKORDER_NOTES     => true,
					self::MANAGE_APPOINTMENTS     => true,
					self::VIEW_APPOINTMENTS       => true,
					self::MANAGE_REMINDERS        => true,
				),
			),
			self::ROLE_FRONT_DESK => array(
				'display_name' => 'PitDesk Front Desk',
				'capabilities' => array(
					// Core read is required for wp-admin access; without it
					// WordPress blocks the dashboard for the user entirely.
					'read'                        => true,
					self::VIEW_CUSTOMERS          => true,
					self::CREATE_CUSTOMERS        => true,
					self::EDIT_CUSTOMERS          => true,
					self::DELETE_CUSTOMERS        => true,
					self::MANAGE_LEADS            => true,
					self::VIEW_VEHICLES           => true,
					self::CREATE_VEHICLES         => true,
					self::EDIT_VEHICLES           => true,
					self::DELETE_VEHICLES         => true,
					self::MANAGE_WORKORDERS       => true,
					self::CHANGE_WORKORDER_STATUS => true,
					self::ADD_WORKORDER_NOTES     => true,
					self::MANAGE_APPOINTMENTS     => true,
					self::VIEW_APPOINTMENTS       => true,
					self::MANAGE_REMINDERS        => true,
				),
			),
			self::ROLE_MECHANIC   => array(
				'display_name' => 'PitDesk Mechanic',
				'capabilities' => array(
					// Core read is required for wp-admin access; without it
					// WordPress blocks the dashboard for the user entirely.
					'read'                        => true,
					// Read only on customers. Menu is hidden in Phase 1 (D-04).
					self::VIEW_CUSTOMERS          => true,
					// The mechanic works the job, not the money (D-20): read
					// vehicles, move a work order through its statuses, and add
					// progress notes, but never create/edit/delete vehicles and
					// never manage (create/delete/price) work orders.
					self::VIEW_VEHICLES           => true,
					self::CHANGE_WORKORDER_STATUS => true,
					self::ADD_WORKORDER_NOTES     => true,
					// View-only on the schedule: the mechanic sees the day's
					// appointments but cannot create, edit, or cancel them.
					self::VIEW_APPOINTMENTS       => true,
				),
			),
		);
	}
}
