<?php
/**
 * Admin dashboard template.
 *
 * @package MerchantKits\AttendanceManager
 */

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

global $wpdb;

$today          = current_time( 'Y-m-d' );
$emp_table      = MKAM_Employee::table();
$att_table      = MKAM_Attendance::table();
$leave_table    = MKAM_Leave::table();

// phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$total_employees = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$emp_table} WHERE status = 'active'" );
$present_today   = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$att_table} WHERE date = %s AND clock_in IS NOT NULL", $today ) );
$on_leave_today  = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$leave_table} WHERE status = 'approved' AND start_date <= %s AND end_date >= %s", $today, $today ) );
$late_today      = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$att_table} WHERE date = %s AND status = 'late'", $today ) );
$pending_leaves  = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$leave_table} WHERE status = 'pending'" );
$absent_today    = max( 0, $total_employees - $present_today - $on_leave_today );

$todays_activity = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$att_table} WHERE date = %s AND clock_in IS NOT NULL ORDER BY clock_in DESC", $today ), ARRAY_A );
$recent_leaves   = $wpdb->get_results( "SELECT * FROM {$leave_table} WHERE status = 'pending' ORDER BY created_at DESC LIMIT 5", ARRAY_A );
// phpcs:enable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared

$cards = array(
	array( 'total', __( 'Total Employees', 'merchantkits-attendance-manager' ), $total_employees ),
	array( 'present', __( 'Present Today', 'merchantkits-attendance-manager' ), $present_today ),
	array( 'leave', __( 'On Leave Today', 'merchantkits-attendance-manager' ), $on_leave_today ),
	array( 'absent', __( 'Absent Today', 'merchantkits-attendance-manager' ), $absent_today ),
	array( 'late', __( 'Late Today', 'merchantkits-attendance-manager' ), $late_today ),
	array( 'pending', __( 'Pending Requests', 'merchantkits-attendance-manager' ), $pending_leaves ),
);
?>
<div class="wrap mkam-dashboard">
	<h1><?php esc_html_e( 'Attendance Manager', 'merchantkits-attendance-manager' ); ?></h1>

	<div class="mkam-stat-grid">
		<?php foreach ( $cards as $card ) : ?>
			<div class="mkam-stat-card mkam-stat-card--<?php echo esc_attr( $card[0] ); ?>">
				<span class="mkam-stat-card__value"><?php echo esc_html( $card[2] ); ?></span>
				<span class="mkam-stat-card__label"><?php echo esc_html( $card[1] ); ?></span>
			</div>
		<?php endforeach; ?>
	</div>

	<div class="mkam-quick-actions">
		<a class="button button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=mkam-employees&action=add' ) ); ?>"><?php esc_html_e( 'Add Employee', 'merchantkits-attendance-manager' ); ?></a>
		<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=mkam-attendance' ) ); ?>"><?php esc_html_e( 'View Attendance', 'merchantkits-attendance-manager' ); ?></a>
		<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=mkam-leaves' ) ); ?>"><?php esc_html_e( 'Review Leaves', 'merchantkits-attendance-manager' ); ?></a>
	</div>

	<div class="mkam-dashboard__columns">
		<div class="mkam-card">
			<h2><?php esc_html_e( "Today's Activity", 'merchantkits-attendance-manager' ); ?></h2>
			<?php if ( empty( $todays_activity ) ) : ?>
				<p><?php esc_html_e( 'No clock-ins recorded today.', 'merchantkits-attendance-manager' ); ?></p>
			<?php else : ?>
				<table class="widefat striped">
					<thead>
						<tr>
							<th><?php esc_html_e( 'Employee', 'merchantkits-attendance-manager' ); ?></th>
							<th><?php esc_html_e( 'Clock In', 'merchantkits-attendance-manager' ); ?></th>
							<th><?php esc_html_e( 'Status', 'merchantkits-attendance-manager' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php
						foreach ( $todays_activity as $row ) :
							$employee = MKAM_Employee::get( $row['employee_id'] );
							?>
							<tr>
								<td><?php echo esc_html( $employee ? MKAM_Employee::full_name( $employee ) : '—' ); ?></td>
								<td><?php echo esc_html( mysql2date( get_option( 'time_format', 'g:i a' ), $row['clock_in'] ) ); ?></td>
								<td>
									<?php if ( (int) $row['late_minutes'] > 0 ) : ?>
										<span class="mkam-badge mkam-badge--late">
											<?php
											/* translators: %d: minutes late. */
											echo esc_html( sprintf( __( 'Late by %d min', 'merchantkits-attendance-manager' ), (int) $row['late_minutes'] ) );
											?>
										</span>
									<?php else : ?>
										<span class="mkam-badge mkam-badge--present"><?php esc_html_e( 'On Time', 'merchantkits-attendance-manager' ); ?></span>
									<?php endif; ?>
								</td>
							</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
			<?php endif; ?>
		</div>

		<div class="mkam-card">
			<h2><?php esc_html_e( 'Recent Leave Requests', 'merchantkits-attendance-manager' ); ?></h2>
			<?php if ( empty( $recent_leaves ) ) : ?>
				<p><?php esc_html_e( 'No pending leave requests.', 'merchantkits-attendance-manager' ); ?></p>
			<?php else : ?>
				<table class="widefat striped">
					<tbody>
						<?php
						foreach ( $recent_leaves as $leave ) :
							$employee   = MKAM_Employee::get( $leave['employee_id'] );
							$policy     = MKAM_Leave::get_policy( $leave['leave_type'] );
							$approve_url = wp_nonce_url( admin_url( 'admin.php?page=mkam-leaves&action=approve&id=' . $leave['id'] ), 'mkam_leave_approve_' . $leave['id'] );
							$reject_url  = wp_nonce_url( admin_url( 'admin.php?page=mkam-leaves&action=reject&id=' . $leave['id'] ), 'mkam_leave_reject_' . $leave['id'] );
							?>
							<tr>
								<td><strong><?php echo esc_html( $employee ? MKAM_Employee::full_name( $employee ) : '—' ); ?></strong></td>
								<td><?php echo esc_html( $policy ? $policy['label'] : $leave['leave_type'] ); ?></td>
								<td><?php echo esc_html( $leave['start_date'] . ' → ' . $leave['end_date'] ); ?></td>
								<td>
									<a class="button button-small button-primary" href="<?php echo esc_url( $approve_url ); ?>"><?php esc_html_e( 'Approve', 'merchantkits-attendance-manager' ); ?></a>
									<a class="button button-small" href="<?php echo esc_url( $reject_url ); ?>"><?php esc_html_e( 'Reject', 'merchantkits-attendance-manager' ); ?></a>
								</td>
							</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
			<?php endif; ?>
		</div>
	</div>

	<?php if ( ! function_exists( 'mkam_fs' ) || mkam_fs()->is_not_paying() ) : ?>
		<div class="mkam-pro-banner">
			<h3><?php esc_html_e( 'Unlock more with Attendance Manager Pro', 'merchantkits-attendance-manager' ); ?></h3>
			<p><?php esc_html_e( 'Shift calendars, payroll exports, advanced AI queries, and more.', 'merchantkits-attendance-manager' ); ?></p>
			<a class="button button-hero" href="<?php echo esc_url( function_exists( 'mkam_fs' ) ? mkam_fs()->get_upgrade_url() : 'https://merchantkits.com/attendance/' ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Upgrade to Pro', 'merchantkits-attendance-manager' ); ?></a>
		</div>
	<?php endif; ?>

	<div class="mkam-cross-promo">
		<?php
		printf(
			/* translators: %s: linked plugin name. */
			esc_html__( 'Need document signatures? Check out %s.', 'merchantkits-attendance-manager' ),
			'<a href="https://wordpress.org/plugins/merchantkits-document-signatures" target="_blank" rel="noopener noreferrer">MerchantKits Document Signatures</a>'
		);
		?>
	</div>

	<?php do_action( 'mkam_admin_dashboard_widgets' ); ?>
</div>
