<?php
// Is script ko seedhe browser mein nahi chalaya ja sakta, yeh server ke liye hai.
// config.php ko shaamil karein taaki hum database se connect kar sakein.
require_once 'init.php';

// Aaj ki tarikh aur din ka naam prapt karein
$today_date = date('Y-m-d');
$today_day_name = date('l'); // Jaise 'Monday', 'Tuesday'

// --- 1. 'Daily' schedule waale task banayein ---
$daily_sql = "SELECT * FROM task_schedules WHERE schedule_type = 'Daily' AND is_active = TRUE";
$daily_schedules = $conn->query($daily_sql);

if ($daily_schedules->num_rows > 0) {
    while ($schedule = $daily_schedules->fetch_assoc()) {
        create_task_from_schedule($conn, $schedule, $today_date);
    }
}
echo "Daily tasks processed.\n";


// --- 2. 'Weekly' schedule waale task banayein ---
$weekly_sql = "SELECT * FROM task_schedules WHERE schedule_type = 'Weekly' AND is_active = TRUE AND FIND_IN_SET(?, assign_day)";
$stmt_weekly = $conn->prepare($weekly_sql);
$stmt_weekly->bind_param("s", $today_day_name);
$stmt_weekly->execute();
$weekly_schedules = $stmt_weekly->get_result();

if ($weekly_schedules->num_rows > 0) {
    while ($schedule = $weekly_schedules->fetch_assoc()) {
        create_task_from_schedule($conn, $schedule, $today_date);
    }
}
$stmt_weekly->close();
echo "Weekly tasks for $today_day_name processed.\n";


// --- 3. 'Alternate' schedule waale task banayein ---
$group_to_run = '';
if (in_array($today_day_name, ['Monday', 'Wednesday', 'Friday'])) {
    $group_to_run = 'GroupA';
} elseif (in_array($today_day_name, ['Tuesday', 'Thursday', 'Saturday'])) {
    $group_to_run = 'GroupB';
}

if (!empty($group_to_run)) {
    $alternate_sql = "SELECT * FROM task_schedules WHERE schedule_type = 'Alternate' AND is_active = TRUE AND assign_day = ?";
    $stmt_alternate = $conn->prepare($alternate_sql);
    $stmt_alternate->bind_param("s", $group_to_run);
    $stmt_alternate->execute();
    $alternate_schedules = $stmt_alternate->get_result();

    if ($alternate_schedules->num_rows > 0) {
        while ($schedule = $alternate_schedules->fetch_assoc()) {
            create_task_from_schedule($conn, $schedule, $today_date);
        }
    }
    $stmt_alternate->close();
    echo "Alternate tasks for $group_to_run processed.\n";
}


// --- Sahayak Function (Helper Function) ---
function create_task_from_schedule($conn, $schedule, $date) {
    // ===== YAHAN BADLAV KIYA GAYA HAI =====
    // Check karein ki is client ke liye aaj ka regular task pehle se to nahi ban gaya hai (chahe kisi ko bhi assign ho)
    $check_sql = "SELECT id FROM tasks WHERE client_id = ? AND task_type = 'Regular' AND assigned_date = ?";
    $stmt_check = $conn->prepare($check_sql);
    $stmt_check->bind_param("is", $schedule['client_id'], $date);
    $stmt_check->execute();
    $result = $stmt_check->get_result();

    if ($result->num_rows == 0) {
        // Agar task nahi bana hai, to naya task banayein
        $client_name_sql = "SELECT name FROM clients WHERE id = ?";
        $client_stmt = $conn->prepare($client_name_sql);
        $client_stmt->bind_param("i", $schedule['client_id']);
        $client_stmt->execute();
        $client_name = $client_stmt->get_result()->fetch_assoc()['name'];
        
        $task_title = "Regular Task for " . $client_name;

        // Graphic Designer ko 'Post' work type ke saath task assign hoga
        $work_type = 'Post';

        $insert_sql = "INSERT INTO tasks (client_id, assigned_user_id, smm_user_id, title, task_type, work_type, assigned_date) VALUES (?, ?, ?, ?, 'Regular', ?, ?)";
        $stmt_insert = $conn->prepare($insert_sql);
        $stmt_insert->bind_param("iissss", $schedule['client_id'], $schedule['user_id'], $schedule['smm_user_id'], $task_title, $work_type, $date);
        $stmt_insert->execute();
        $stmt_insert->close();
        $client_stmt->close();
        echo "Created task for client ID: " . $schedule['client_id'] . "\n";
    } else {
        echo "Skipped task for client ID: " . $schedule['client_id'] . " (already exists for today).\n";
    }
    $stmt_check->close();
}

$conn->close();
echo "All tasks for $today_date have been processed.\n";
?>
