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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Solver service for Employee Scheduling.
//!
//! Uses Late Acceptance local search with change moves.
//! Incremental scoring via TypedScoreDirector for O(1) move evaluation.

use parking_lot::RwLock;
use rand::Rng;
use solverforge::prelude::*;
use solverforge::TypedScoreDirector;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::oneshot;
use tracing::{debug, info};

use crate::console::{self, PhaseTimer};
use crate::constraints::create_fluent_constraints;
use crate::domain::EmployeeSchedule;

/// Default solving time: 30 seconds.
const DEFAULT_TIME_LIMIT_SECS: u64 = 30;

/// Late acceptance history size.
const LATE_ACCEPTANCE_SIZE: usize = 400;

/// Solver configuration with termination criteria.
#[derive(Debug, Clone, Default)]
pub struct SolverConfig {
    /// Stop after this duration.
    pub time_limit: Option<Duration>,
    /// Stop after this duration without improvement.
    pub unimproved_time_limit: Option<Duration>,
    /// Stop after this many steps.
    pub step_limit: Option<u64>,
    /// Stop after this many steps without improvement.
    pub unimproved_step_limit: Option<u64>,
}

impl SolverConfig {
    /// Creates a config with default 30-second time limit.
    pub fn default_config() -> Self {
        Self {
            time_limit: Some(Duration::from_secs(DEFAULT_TIME_LIMIT_SECS)),
            ..Default::default()
        }
    }

    /// Checks if any termination condition is met.
    fn should_terminate(
        &self,
        elapsed: Duration,
        steps: u64,
        time_since_improvement: Duration,
        steps_since_improvement: u64,
    ) -> bool {
        if let Some(limit) = self.time_limit {
            if elapsed >= limit {
                return true;
            }
        }
        if let Some(limit) = self.unimproved_time_limit {
            if time_since_improvement >= limit {
                return true;
            }
        }
        if let Some(limit) = self.step_limit {
            if steps >= limit {
                return true;
            }
        }
        if let Some(limit) = self.unimproved_step_limit {
            if steps_since_improvement >= limit {
                return true;
            }
        }
        false
    }
}

/// Status of a solving job.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum SolverStatus {
    /// Not currently solving.
    NotSolving,
    /// Actively solving.
    Solving,
}

impl SolverStatus {
    /// Returns the status as a SCREAMING_SNAKE_CASE string for API responses.
    ///
    /// ```
    /// use employee_scheduling::solver::SolverStatus;
    ///
    /// assert_eq!(SolverStatus::NotSolving.as_str(), "NOT_SOLVING");
    /// assert_eq!(SolverStatus::Solving.as_str(), "SOLVING");
    /// ```
    pub fn as_str(self) -> &'static str {
        match self {
            SolverStatus::NotSolving => "NOT_SOLVING",
            SolverStatus::Solving => "SOLVING",
        }
    }
}

/// A solving job with current state.
pub struct SolveJob {
    /// Unique job identifier.
    pub id: String,
    /// Current status.
    pub status: SolverStatus,
    /// Current best schedule.
    pub schedule: EmployeeSchedule,
    /// Solver configuration.
    pub config: SolverConfig,
    /// Stop signal sender.
    stop_signal: Option<oneshot::Sender<()>>,
}

impl SolveJob {
    /// Creates a new solve job with default config.
    pub fn new(id: String, schedule: EmployeeSchedule) -> Self {
        Self {
            id,
            status: SolverStatus::NotSolving,
            schedule,
            config: SolverConfig::default_config(),
            stop_signal: None,
        }
    }

    /// Creates a new solve job with custom config.
    pub fn with_config(id: String, schedule: EmployeeSchedule, config: SolverConfig) -> Self {
        Self {
            id,
            status: SolverStatus::NotSolving,
            schedule,
            config,
            stop_signal: None,
        }
    }
}

/// Manages Employee Scheduling solving jobs.
///
/// # Examples
///
/// ```
/// use employee_scheduling::solver::SolverService;
/// use employee_scheduling::demo_data::{generate, DemoData};
///
/// let service = SolverService::new();
/// let schedule = generate(DemoData::Small);
///
/// // Create a job (doesn't start solving yet)
/// let job = service.create_job("test-1".to_string(), schedule);
/// assert_eq!(job.read().status, employee_scheduling::solver::SolverStatus::NotSolving);
/// ```
pub struct SolverService {
    jobs: RwLock<HashMap<String, Arc<RwLock<SolveJob>>>>,
}

impl SolverService {
    /// Creates a new solver service.
    pub fn new() -> Self {
        Self {
            jobs: RwLock::new(HashMap::new()),
        }
    }

    /// Creates a new job for the given schedule with default config.
    pub fn create_job(&self, id: String, schedule: EmployeeSchedule) -> Arc<RwLock<SolveJob>> {
        let job = Arc::new(RwLock::new(SolveJob::new(id.clone(), schedule)));
        self.jobs.write().insert(id, job.clone());
        job
    }

    /// Creates a new job with custom config.
    pub fn create_job_with_config(
        &self,
        id: String,
        schedule: EmployeeSchedule,
        config: SolverConfig,
    ) -> Arc<RwLock<SolveJob>> {
        let job = Arc::new(RwLock::new(SolveJob::with_config(id.clone(), schedule, config)));
        self.jobs.write().insert(id, job.clone());
        job
    }

    /// Gets a job by ID.
    pub fn get_job(&self, id: &str) -> Option<Arc<RwLock<SolveJob>>> {
        self.jobs.read().get(id).cloned()
    }

    /// Lists all job IDs.
    pub fn list_jobs(&self) -> Vec<String> {
        self.jobs.read().keys().cloned().collect()
    }

    /// Removes a job by ID.
    pub fn remove_job(&self, id: &str) -> Option<Arc<RwLock<SolveJob>>> {
        self.jobs.write().remove(id)
    }

    /// Starts solving a job in the background.
    pub fn start_solving(&self, job: Arc<RwLock<SolveJob>>) {
        let (tx, rx) = oneshot::channel();
        let config = job.read().config.clone();

        {
            let mut job_guard = job.write();
            job_guard.status = SolverStatus::Solving;
            job_guard.stop_signal = Some(tx);
        }

        let job_clone = job.clone();

        tokio::task::spawn_blocking(move || {
            solve_blocking(job_clone, rx, config);
        });
    }

    /// Stops a solving job.
    pub fn stop_solving(&self, id: &str) -> bool {
        if let Some(job) = self.get_job(id) {
            let mut job_guard = job.write();
            if let Some(stop_signal) = job_guard.stop_signal.take() {
                let _ = stop_signal.send(());
                job_guard.status = SolverStatus::NotSolving;
                return true;
            }
        }
        false
    }
}

impl Default for SolverService {
    fn default() -> Self {
        Self::new()
    }
}

/// Runs the solver in a blocking context.
fn solve_blocking(
    job: Arc<RwLock<SolveJob>>,
    mut stop_rx: oneshot::Receiver<()>,
    config: SolverConfig,
) {
    let initial_schedule = job.read().schedule.clone();
    let job_id = job.read().id.clone();
    let solve_start = Instant::now();

    // Print problem configuration
    console::print_config(
        initial_schedule.shifts.len(),
        initial_schedule.employees.len(),
    );

    info!(
        job_id = %job_id,
        shifts = initial_schedule.shifts.len(),
        employees = initial_schedule.employees.len(),
        "Starting Employee Scheduling solver"
    );

    // Create typed constraints and score director
    let constraints = create_fluent_constraints();
    let mut director = TypedScoreDirector::new(initial_schedule.clone(), constraints);

    // Phase 1: Construction heuristic (round-robin)
    let mut ch_timer = PhaseTimer::start("ConstructionHeuristic", 0);
    let mut current_score = construction_heuristic(&mut director, &mut ch_timer);
    ch_timer.finish();

    // Print solving started after construction
    console::print_solving_started(
        solve_start.elapsed().as_millis() as u64,
        &current_score.to_string(),
        initial_schedule.shifts.len(),
        initial_schedule.shifts.len(),
        initial_schedule.employees.len(),
    );

    // Update job with constructed solution
    update_job(&job, &director, current_score);

    // Phase 2: Late Acceptance local search
    let n_employees = director.working_solution().employees.len();
    if n_employees == 0 {
        info!("No employees to optimize");
        console::print_solving_ended(
            solve_start.elapsed(),
            0,
            1,
            &current_score.to_string(),
            current_score.is_feasible(),
        );
        finish_job(&job, &director, current_score);
        return;
    }

    let mut ls_timer = PhaseTimer::start("LateAcceptance", 1);
    let mut late_scores = vec![current_score; LATE_ACCEPTANCE_SIZE];
    let mut step: u64 = 0;
    let mut rng = rand::thread_rng();

    // Track best score and improvement times
    let mut best_score = current_score;
    let mut last_improvement_time = solve_start;
    let mut last_improvement_step: u64 = 0;

    loop {
        // Check termination conditions
        let elapsed = solve_start.elapsed();
        let time_since_improvement = last_improvement_time.elapsed();
        let steps_since_improvement = step - last_improvement_step;

        if config.should_terminate(elapsed, step, time_since_improvement, steps_since_improvement) {
            debug!("Termination condition met");
            break;
        }

        // Check for stop signal
        if stop_rx.try_recv().is_ok() {
            info!("Solving terminated early by user");
            break;
        }

        // Generate random change move
        if let Some((shift_idx, new_employee_idx)) = generate_move(&director, &mut rng) {
            ls_timer.record_move();

            // Try the move
            let old_score = current_score;
            let old_employee_idx = apply_move(&mut director, shift_idx, new_employee_idx);
            let new_score = director.get_score();

            // Late acceptance criterion
            let late_idx = (step as usize) % LATE_ACCEPTANCE_SIZE;
            let late_score = late_scores[late_idx];

            if new_score >= old_score || new_score >= late_score {
                // Accept
                ls_timer.record_accepted(&current_score.to_string());
                current_score = new_score;
                late_scores[late_idx] = new_score;

                // Track improvements
                if new_score > best_score {
                    best_score = new_score;
                    last_improvement_time = Instant::now();
                    last_improvement_step = step;
                }

                // Periodic update
                if ls_timer.steps_accepted().is_multiple_of(1000) {
                    update_job(&job, &director, current_score);
                    debug!(
                        step,
                        moves_accepted = ls_timer.steps_accepted(),
                        score = %current_score,
                        elapsed_secs = solve_start.elapsed().as_secs(),
                        "Progress update"
                    );
                }

                // Periodic console progress (every 10000 moves)
                if ls_timer.moves_evaluated().is_multiple_of(10000) {
                    console::print_step_progress(
                        ls_timer.steps_accepted(),
                        ls_timer.elapsed(),
                        ls_timer.moves_evaluated(),
                        &current_score.to_string(),
                    );
                }
            } else {
                // Reject - undo
                undo_move(&mut director, shift_idx, old_employee_idx);
            }

            step += 1;
        }
    }

    ls_timer.finish();

    let total_duration = solve_start.elapsed();

    info!(
        job_id = %job_id,
        duration_secs = total_duration.as_secs_f64(),
        steps = step,
        score = %current_score,
        feasible = current_score.is_feasible(),
        "Solving complete"
    );

    console::print_solving_ended(
        total_duration,
        step,
        2,
        &current_score.to_string(),
        current_score.is_feasible(),
    );

    finish_job(&job, &director, current_score);
}

/// Construction heuristic: round-robin employee assignment.
fn construction_heuristic(
    director: &mut TypedScoreDirector<EmployeeSchedule, impl ConstraintSet<EmployeeSchedule, HardSoftDecimalScore>>,
    timer: &mut PhaseTimer,
) -> HardSoftDecimalScore {
    // Initialize score
    let _ = director.calculate_score();

    let n_shifts = director.working_solution().shifts.len();
    let n_employees = director.working_solution().employees.len();

    if n_employees == 0 || n_shifts == 0 {
        return director.get_score();
    }

    // Count already-assigned shifts
    let assigned_count = director
        .working_solution()
        .shifts
        .iter()
        .filter(|s| s.employee_idx.is_some())
        .count();

    // If all shifts already assigned, skip construction
    if assigned_count == n_shifts {
        info!("All shifts already assigned, skipping construction heuristic");
        return director.get_score();
    }

    // Round-robin assignment for unassigned shifts only
    let mut employee_idx = 0;
    for shift_idx in 0..n_shifts {
        if director.working_solution().shifts[shift_idx].employee_idx.is_some() {
            continue;
        }

        timer.record_move();
        director.before_variable_changed(shift_idx);
        director.working_solution_mut().shifts[shift_idx].employee_idx = Some(employee_idx);
        director.after_variable_changed(shift_idx);

        let score = director.get_score();
        timer.record_accepted(&score.to_string());

        employee_idx = (employee_idx + 1) % n_employees;
    }

    director.get_score()
}

/// Generates a random change move (assign a different employee to a shift).
fn generate_move<R: Rng>(
    director: &TypedScoreDirector<EmployeeSchedule, impl ConstraintSet<EmployeeSchedule, HardSoftDecimalScore>>,
    rng: &mut R,
) -> Option<(usize, Option<usize>)> {
    let solution = director.working_solution();
    let n_shifts = solution.shifts.len();
    let n_employees = solution.employees.len();

    if n_shifts == 0 || n_employees == 0 {
        return None;
    }

    // Pick random shift
    let shift_idx = rng.gen_range(0..n_shifts);
    let current_employee = solution.shifts[shift_idx].employee_idx;

    // Pick random new employee (different from current)
    let new_employee_idx = rng.gen_range(0..n_employees);

    // Skip no-op moves
    if current_employee == Some(new_employee_idx) {
        return None;
    }

    Some((shift_idx, Some(new_employee_idx)))
}

/// Applies a change move, returns the old employee index.
fn apply_move(
    director: &mut TypedScoreDirector<EmployeeSchedule, impl ConstraintSet<EmployeeSchedule, HardSoftDecimalScore>>,
    shift_idx: usize,
    new_employee_idx: Option<usize>,
) -> Option<usize> {
    let old_employee_idx = director.working_solution().shifts[shift_idx].employee_idx;

    director.before_variable_changed(shift_idx);
    director.working_solution_mut().shifts[shift_idx].employee_idx = new_employee_idx;
    director.after_variable_changed(shift_idx);

    old_employee_idx
}

/// Undoes a change move.
fn undo_move(
    director: &mut TypedScoreDirector<EmployeeSchedule, impl ConstraintSet<EmployeeSchedule, HardSoftDecimalScore>>,
    shift_idx: usize,
    old_employee_idx: Option<usize>,
) {
    director.before_variable_changed(shift_idx);
    director.working_solution_mut().shifts[shift_idx].employee_idx = old_employee_idx;
    director.after_variable_changed(shift_idx);
}

/// Updates job with current solution.
fn update_job(
    job: &Arc<RwLock<SolveJob>>,
    director: &TypedScoreDirector<EmployeeSchedule, impl ConstraintSet<EmployeeSchedule, HardSoftDecimalScore>>,
    score: HardSoftDecimalScore,
) {
    let mut job_guard = job.write();
    job_guard.schedule = director.clone_working_solution();
    job_guard.schedule.score = Some(score);
}

/// Finishes job and sets status.
fn finish_job(
    job: &Arc<RwLock<SolveJob>>,
    director: &TypedScoreDirector<EmployeeSchedule, impl ConstraintSet<EmployeeSchedule, HardSoftDecimalScore>>,
    score: HardSoftDecimalScore,
) {
    let mut job_guard = job.write();
    job_guard.schedule = director.clone_working_solution();
    job_guard.schedule.score = Some(score);
    job_guard.status = SolverStatus::NotSolving;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::demo_data::{generate, DemoData};

    #[test]
    fn test_construction_heuristic() {
        let schedule = generate(DemoData::Small);
        let constraints = create_fluent_constraints();
        let mut director = TypedScoreDirector::new(schedule, constraints);

        let mut timer = PhaseTimer::start("ConstructionHeuristic", 0);
        let score = construction_heuristic(&mut director, &mut timer);

        // All shifts should be assigned
        let assigned_count = director
            .working_solution()
            .shifts
            .iter()
            .filter(|s| s.employee_idx.is_some())
            .count();
        let total_shifts = director.working_solution().shifts.len();
        assert_eq!(assigned_count, total_shifts);
        assert!(score.hard_scaled() <= 0); // May have some violations
    }
}