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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//! Contains all the actions that can be broadcasted via [`crate::sse::broadcaster::Broadcaster`].

// The properties of the actions are not documented because they are
// self-explanatory and should be already documented in the respective
// entity DTOs.
#![allow(clippy::missing_docs_in_private_items)]
// Don't make the `new` functions const, there might come more fields in the future.
#![allow(clippy::missing_const_for_fn)]

use chrono::NaiveDate;
use postgis_diesel::types::{Point, Polygon};
use serde::Serialize;
use typeshare::typeshare;
use uuid::Uuid;

use crate::model::{
    dto::{
        base_layer_images::BaseLayerImageDto,
        drawings::DrawingDto,
        layers::{LayerDto, LayerRenameDto},
        plantings::{
            DeletePlantingDto, MovePlantingDto, PlantingDto, TransformPlantingDto,
            UpdateAddDatePlantingDto, UpdatePlantingNoteDto, UpdateRemoveDatePlantingDto,
        },
        shadings::{
            DeleteShadingDto, ShadingDto, UpdateAddDateShadingDto, UpdateNotesShadingDto,
            UpdateRemoveDateShadingDto, UpdateValuesShadingDto,
        },
        UpdateMapGeometryDto,
    },
    r#enum::shade::Shade,
};

#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Action {
    pub action_id: Uuid,
    pub user_id: Uuid,
    pub action: ActionType,
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
// Use the name of the enum variant as the type field looking like { "type": "CreatePlanting", ... }.
/// An enum representing all the actions that can be broadcasted via [`crate::sse::broadcaster::Broadcaster`].
#[serde(tag = "type", content = "payload")]
pub enum ActionType {
    /// An action used to broadcast creation of a layer.
    CreateLayer(LayerDto),
    /// An action used to broadcast soft-deletion of a layer.
    /// Deleted layers can be restored.
    DeleteLayer(Uuid),
    /// An action used to broadcast restoration of a deleted layer.
    /// Restore is only supported for drawing layers
    RestoreDrawingLayer(RestoreDrawingLayerActionPayload),
    /// An action used to broadcast the renaming of a layer.
    RenameLayer(LayerRenameDto),
    /// An action used to broadcast the reordering of multiple layers.
    ReorderLayers(Vec<Uuid>),

    /// An action used to broadcast creation of plantings.
    CreatePlanting(Vec<PlantingDto>),
    /// An action used to broadcast deletion of plantings.
    DeletePlanting(Vec<DeletePlantingActionPayload>),
    /// An action used to broadcast movement of plantings.
    MovePlanting(Vec<MovePlantingActionPayload>),
    /// An action used to broadcast transformation of plantings.
    TransformPlanting(Vec<TransformPlantingActionPayload>),
    /// An action used to update the `add_date` of plantings.
    UpdatePlantingAddDate(Vec<UpdateAddDateActionPayload>),
    /// An action used to update the `remove_date` of plantings.
    UpdatePlantingRemoveDate(Vec<UpdateRemoveDateActionPayload>),
    /// An action used to broadcast updating Markdown notes of plantings.
    UpdatePlantingNotes(Vec<UpdatePlantingNotesActionPayload>),

    /// An action used to broadcast creation of shadings.
    CreateShading(Vec<CreateShadingActionPayload>),
    /// An action used to broadcast deletion of shadings.
    DeleteShading(Vec<DeleteShadingActionPayload>),
    /// An action used to broadcast change of shadings.
    UpdateShading(Vec<UpdateShadingActionPayload>),
    /// An action used to update `add_date` of shadings.
    UpdateShadingAddDate(Vec<UpdateShadingAddDateActionPayload>),
    /// An action used to update `remove_date` of shadings.
    UpdateShadingRemoveDate(Vec<UpdateShadingRemoveDateActionPayload>),
    /// An actin used to update `notes` of shadings.
    UpdateShadingNotes(Vec<UpdateShadingNotesActionPayload>),

    /// An action used to broadcast creation of a baseLayerImage.
    CreateBaseLayerImage(CreateBaseLayerImageActionPayload),
    /// An action used to broadcast update of a baseLayerImage.
    UpdateBaseLayerImage(UpdateBaseLayerImageActionPayload),
    /// An action used to broadcast deletion of a baseLayerImage.
    DeleteBaseLayerImage(DeleteBaseLayerImageActionPayload),
    /// An action used to broadcast an update to the map geometry.
    UpdateMapGeometry(UpdateMapGeometryActionPayload),
    /// An action used to update `additional_name` of one planting.
    UpdatePlantingAdditionalName(UpdatePlantingAdditionalNamePayload),

    /// An action used to broadcast creation of new drawings.
    CreateDrawing(Vec<DrawingDto>),
    /// An action used to broadcast deletion of an existing drawings.
    DeleteDrawing(Vec<Uuid>),
    /// An action used to broadcast arbitrary updates of existing drawings.
    UpdateDrawing(Vec<DrawingDto>),
    /// An action used to broadcast the update of `add_date` of existing drawings.
    UpdateDrawingAddDate(Vec<DrawingDto>),
    /// An action used to broadcast the update of `remove_date` of existing drawings.
    UpdateDrawingRemoveDate(Vec<DrawingDto>),
    /// An action used to broadcast the update of `notes` of existing drawings.
    UpdateDrawingNotes(Vec<UpdateDrawingNotesActionPayload>),
}

impl Action {
    #[must_use]
    pub fn new_create_planting_action(
        dtos: Vec<PlantingDto>,
        user_id: Uuid,
        action_id: Uuid,
    ) -> Self {
        Self {
            action_id,
            user_id,
            action: ActionType::CreatePlanting(dtos),
        }
    }

    #[must_use]
    pub fn new_delete_planting_action(
        dtos: &[DeletePlantingDto],
        user_id: Uuid,
        action_id: Uuid,
    ) -> Self {
        Self {
            action_id,
            user_id,
            action: ActionType::DeletePlanting(
                dtos.iter()
                    .map(|dto| DeletePlantingActionPayload { id: dto.id })
                    .collect(),
            ),
        }
    }

    #[must_use]
    pub fn new_move_planting_action(
        dtos: &[MovePlantingDto],
        user_id: Uuid,
        action_id: Uuid,
    ) -> Self {
        Self {
            action_id,
            user_id,
            action: ActionType::MovePlanting(
                dtos.iter()
                    .map(|dto| MovePlantingActionPayload {
                        id: dto.id,
                        x: dto.x,
                        y: dto.y,
                    })
                    .collect(),
            ),
        }
    }

    #[must_use]
    pub fn new_transform_planting_action(
        dtos: &[TransformPlantingDto],
        user_id: Uuid,
        action_id: Uuid,
    ) -> Self {
        Self {
            action_id,
            user_id,
            action: ActionType::TransformPlanting(
                dtos.iter()
                    .map(|dto| TransformPlantingActionPayload {
                        id: dto.id,
                        x: dto.x,
                        y: dto.y,
                        rotation: dto.rotation,
                        size_x: dto.size_x,
                        size_y: dto.size_y,
                    })
                    .collect(),
            ),
        }
    }

    #[must_use]
    pub fn new_update_planting_add_date_action(
        dtos: &[UpdateAddDatePlantingDto],
        user_id: Uuid,
        action_id: Uuid,
    ) -> Self {
        Self {
            action_id,
            user_id,
            action: ActionType::UpdatePlantingAddDate(
                dtos.iter()
                    .map(|dto| UpdateAddDateActionPayload {
                        id: dto.id,
                        add_date: dto.add_date,
                    })
                    .collect(),
            ),
        }
    }

    #[must_use]
    pub fn new_update_planting_remove_date_action(
        dtos: &[UpdateRemoveDatePlantingDto],
        user_id: Uuid,
        action_id: Uuid,
    ) -> Self {
        Self {
            action_id,
            user_id,
            action: ActionType::UpdatePlantingRemoveDate(
                dtos.iter()
                    .map(|dto| UpdateRemoveDateActionPayload {
                        id: dto.id,
                        remove_date: dto.remove_date,
                    })
                    .collect(),
            ),
        }
    }

    #[must_use]
    pub fn new_update_planting_note_action(
        dtos: &[UpdatePlantingNoteDto],
        user_id: Uuid,
        action_id: Uuid,
    ) -> Self {
        Self {
            action_id,
            user_id,
            action: ActionType::UpdatePlantingNotes(
                dtos.iter()
                    .map(|dto| UpdatePlantingNotesActionPayload {
                        id: dto.id,
                        notes: dto.notes.clone(),
                    })
                    .collect(),
            ),
        }
    }

    #[must_use]
    pub fn new_create_shading_action(dtos: &[ShadingDto], user_id: Uuid, action_id: Uuid) -> Self {
        Self {
            action_id,
            user_id,
            action: ActionType::CreateShading(
                dtos.iter()
                    .map(|dto| CreateShadingActionPayload {
                        id: dto.id,
                        layer_id: dto.layer_id,
                        shade: dto.shade,
                        add_date: dto.add_date,
                        remove_date: dto.remove_date,
                        geometry: dto.geometry.clone(),
                        notes: dto.notes.clone(),
                    })
                    .collect(),
            ),
        }
    }

    #[must_use]
    pub fn new_delete_shading_action(
        dtos: &[DeleteShadingDto],
        user_id: Uuid,
        action_id: Uuid,
    ) -> Self {
        Self {
            action_id,
            user_id,
            action: ActionType::DeleteShading(
                dtos.iter()
                    .map(|dto| DeleteShadingActionPayload { id: dto.id })
                    .collect(),
            ),
        }
    }

    #[must_use]
    pub fn new_update_shading_action(
        dtos: &[UpdateValuesShadingDto],
        user_id: Uuid,
        action_id: Uuid,
    ) -> Self {
        Self {
            action_id,
            user_id,
            action: ActionType::UpdateShading(
                dtos.iter()
                    .map(|dto| UpdateShadingActionPayload {
                        id: dto.id,
                        shade: dto.shade,
                        geometry: dto.clone().geometry,
                    })
                    .collect(),
            ),
        }
    }

    #[must_use]
    pub fn new_update_shading_remove_date_action(
        dtos: &[UpdateRemoveDateShadingDto],
        user_id: Uuid,
        action_id: Uuid,
    ) -> Self {
        Self {
            action_id,
            user_id,
            action: ActionType::UpdateShadingRemoveDate(
                dtos.iter()
                    .map(|dto| UpdateShadingRemoveDateActionPayload {
                        id: dto.id,
                        remove_date: dto.remove_date,
                    })
                    .collect(),
            ),
        }
    }

    #[must_use]
    pub fn new_update_shading_add_date_action(
        dtos: &[UpdateAddDateShadingDto],
        user_id: Uuid,
        action_id: Uuid,
    ) -> Self {
        Self {
            action_id,
            user_id,
            action: ActionType::UpdateShadingAddDate(
                dtos.iter()
                    .map(|dto| UpdateShadingAddDateActionPayload {
                        id: dto.id,
                        add_date: dto.add_date,
                    })
                    .collect(),
            ),
        }
    }

    #[must_use]
    pub fn new_update_shading_add_notes(
        dtos: &[UpdateNotesShadingDto],
        user_id: Uuid,
        action_id: Uuid,
    ) -> Self {
        Self {
            action_id,
            user_id,
            action: ActionType::UpdateShadingNotes(
                dtos.iter()
                    .map(|dto| UpdateShadingNotesActionPayload {
                        id: dto.id,
                        notes: dto.notes.clone(),
                    })
                    .collect(),
            ),
        }
    }
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::RestoreDrawingLayer`].
#[serde(rename_all = "camelCase")]
pub struct RestoreDrawingLayerActionPayload {
    // ID of the layer
    id: Uuid,
    // Drawings saved on that layer
    drawings: Vec<DrawingDto>,
}

impl RestoreDrawingLayerActionPayload {
    #[must_use]
    pub fn new(id: Uuid, drawings: Vec<DrawingDto>) -> Self {
        Self { id, drawings }
    }
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::CreatePlanting`].
/// This struct should always match [`PlantingDto`].
#[serde(rename_all = "camelCase")]
pub struct CreatePlantActionPayload {
    id: Uuid,
    layer_id: Uuid,
    plant_id: i32,
    x: i32,
    y: i32,
    rotation: f32,
    size_x: i32,
    size_y: i32,
    add_date: Option<NaiveDate>,
    remove_date: Option<NaiveDate>,
    seed_id: Option<i32>,
    additional_name: Option<String>,
    is_area: bool,
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::DeletePlanting`].
#[serde(rename_all = "camelCase")]
pub struct DeletePlantingActionPayload {
    id: Uuid,
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::MovePlanting`].
#[serde(rename_all = "camelCase")]
pub struct MovePlantingActionPayload {
    id: Uuid,
    x: i32,
    y: i32,
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::TransformPlanting`].
#[serde(rename_all = "camelCase")]
pub struct TransformPlantingActionPayload {
    id: Uuid,
    x: i32,
    y: i32,
    rotation: f32,
    size_x: i32,
    size_y: i32,
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CreateShadingActionPayload {
    id: Uuid,
    layer_id: Uuid,
    shade: Shade,
    geometry: Polygon<Point>,
    add_date: Option<NaiveDate>,
    remove_date: Option<NaiveDate>,
    notes: String,
}

impl CreateShadingActionPayload {
    #[must_use]
    pub fn new(payload: ShadingDto) -> Self {
        Self {
            id: payload.id,
            layer_id: payload.layer_id,
            shade: payload.shade,
            geometry: payload.geometry,
            add_date: payload.add_date,
            remove_date: payload.remove_date,
            notes: payload.notes,
        }
    }
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::UpdateShading`].
#[serde(rename_all = "camelCase")]
pub struct UpdateShadingActionPayload {
    id: Uuid,
    shade: Option<Shade>,
    geometry: Option<Polygon<Point>>,
}

impl UpdateShadingActionPayload {
    #[must_use]
    pub fn new(payload: ShadingDto) -> Self {
        Self {
            id: payload.id,
            shade: Some(payload.shade),
            geometry: Some(payload.geometry),
        }
    }
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::DeleteShading`].
#[serde(rename_all = "camelCase")]
pub struct DeleteShadingActionPayload {
    id: Uuid,
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::UpdateShadingAddDate`].
#[serde(rename_all = "camelCase")]
pub struct UpdateShadingAddDateActionPayload {
    id: Uuid,
    add_date: Option<NaiveDate>,
}

impl UpdateShadingAddDateActionPayload {
    #[must_use]
    pub fn new(payload: &ShadingDto) -> Self {
        Self {
            id: payload.id,
            add_date: payload.add_date,
        }
    }
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::UpdateShadingRemoveDate`].
#[serde(rename_all = "camelCase")]
pub struct UpdateShadingRemoveDateActionPayload {
    id: Uuid,
    remove_date: Option<NaiveDate>,
}

impl UpdateShadingRemoveDateActionPayload {
    #[must_use]
    pub fn new(payload: &ShadingDto) -> Self {
        Self {
            id: payload.id,
            remove_date: payload.remove_date,
        }
    }
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::UpdateShadingNotes`].
pub struct UpdateShadingNotesActionPayload {
    id: Uuid,
    notes: String,
}

impl UpdateShadingNotesActionPayload {
    #[must_use]
    pub fn new(payload: &ShadingDto) -> Self {
        Self {
            id: payload.id,
            notes: payload.notes.clone(),
        }
    }
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::UpdatePlantingNotes`].
#[serde(rename_all = "camelCase")]
pub struct UpdatePlantingNotesActionPayload {
    id: Uuid,
    notes: String,
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::CreateBaseLayerImage`].
/// This struct should always match [`BaseLayerImageDto`].
#[serde(rename_all = "camelCase")]
pub struct CreateBaseLayerImageActionPayload {
    id: Uuid,
    layer_id: Uuid,
    rotation: f32,
    scale: f32,
    path: String,
}

impl CreateBaseLayerImageActionPayload {
    #[must_use]
    pub fn new(payload: BaseLayerImageDto) -> Self {
        Self {
            id: payload.id,
            layer_id: payload.layer_id,
            rotation: payload.rotation,
            scale: payload.scale,
            path: payload.path,
        }
    }
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::DeleteBaseLayerImage`].
#[serde(rename_all = "camelCase")]
pub struct DeleteBaseLayerImageActionPayload {
    id: Uuid,
}

impl DeleteBaseLayerImageActionPayload {
    #[must_use]
    pub fn new(id: Uuid) -> Self {
        Self { id }
    }
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::UpdateBaseLayerImage`].
#[serde(rename_all = "camelCase")]
pub struct UpdateBaseLayerImageActionPayload {
    id: Uuid,
    layer_id: Uuid,
    rotation: f32,
    scale: f32,
    path: String,
    x: i32,
    y: i32,
}

impl UpdateBaseLayerImageActionPayload {
    #[must_use]
    pub fn new(payload: BaseLayerImageDto) -> Self {
        Self {
            id: payload.id,
            layer_id: payload.layer_id,
            rotation: payload.rotation,
            scale: payload.scale,
            path: payload.path,
            x: payload.x,
            y: payload.y,
        }
    }
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the actions [`ActionType::UpdatePlantingAddDate]`].
#[serde(rename_all = "camelCase")]
pub struct UpdateAddDateActionPayload {
    id: Uuid,
    add_date: Option<NaiveDate>,
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::UpdatePlantingRemoveDate`].
#[serde(rename_all = "camelCase")]
pub struct UpdateRemoveDateActionPayload {
    id: Uuid,
    remove_date: Option<NaiveDate>,
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::UpdateMapGeometry`].
#[serde(rename_all = "camelCase")]
pub struct UpdateMapGeometryActionPayload {
    /// The entity id for this action.
    map_id: i32,
    // E.g. `{"rings": [[{"x": 0.0,"y": 0.0},{"x": 1000.0,"y": 0.0},{"x": 1000.0,"y": 1000.0},{"x": 0.0,"y": 1000.0},{"x": 0.0,"y": 0.0}]],"srid": 4326}`
    #[typeshare(serialized_as = "object")]
    geometry: Polygon<Point>,
}

impl UpdateMapGeometryActionPayload {
    #[must_use]
    pub fn new(payload: UpdateMapGeometryDto, map_id: i32) -> Self {
        Self {
            map_id,
            geometry: payload.geometry,
        }
    }
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::UpdatePlantingRemoveDate`].
#[serde(rename_all = "camelCase")]
pub struct UpdatePlantingAdditionalNamePayload {
    id: Uuid,
    additional_name: Option<String>,
}

impl UpdatePlantingAdditionalNamePayload {
    #[must_use]
    pub fn new(payload: &PlantingDto, new_additional_name: Option<String>) -> Self {
        Self {
            id: payload.id,
            additional_name: new_additional_name,
        }
    }
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::UpdatePlantingNotes`].
#[serde(rename_all = "camelCase")]
pub struct UpdateDrawingNotesActionPayload {
    id: Uuid,
    notes: String,
}

impl UpdateDrawingNotesActionPayload {
    #[must_use]
    pub fn new(drawing: DrawingDto) -> Self {
        Self {
            id: drawing.id,
            notes: drawing.notes,
        }
    }
}