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
//! 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::{
    drawings::DrawingDto,
    layers::{LayerDto, LayerRenameDto},
    plantings::{
        DeletePlantingDto, MovePlantingDto, PlantingDto, TransformPlantingDto,
        UpdateAddDatePlantingDto, UpdatePlantingNoteDto, UpdateRemoveDatePlantingDto,
    },
    BaseLayerImageDto, UpdateMapGeometryDto,
};

#[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 a plant.
    CreatePlanting(Vec<PlantingDto>),
    /// An action used to broadcast deletion of a plant.
    DeletePlanting(Vec<DeletePlantActionPayload>),
    /// An action used to broadcast movement of a plant.
    MovePlanting(Vec<MovePlantActionPayload>),
    /// An action used to broadcast transformation of a plant.
    TransformPlanting(Vec<TransformPlantActionPayload>),
    /// An action used to update the `add_date` of a plant.
    UpdatePlantingAddDate(Vec<UpdateAddDateActionPayload>),
    /// An action used to update the `remove_date` of a plant.
    UpdatePlantingRemoveDate(Vec<UpdateRemoveDateActionPayload>),
    /// An action used to broadcast updating a Markdown notes of a plant.
    UpdatePlatingNotes(Vec<UpdatePlantingNoteActionPayload>),
    /// 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 the `additional_name` of a plant.
    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<DrawingDto>),
}

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| DeletePlantActionPayload { 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| MovePlantActionPayload {
                        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| TransformPlantActionPayload {
                        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::UpdatePlatingNotes(
                dtos.iter()
                    .map(|dto| UpdatePlantingNoteActionPayload {
                        id: dto.id,
                        notes: dto.notes.clone(),
                    })
                    .collect(),
            ),
        }
    }
}

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::CreatePlanting`].
/// This struct should always match [`PlantingDto`].
#[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 DeletePlantActionPayload {
    id: Uuid,
}

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

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

#[typeshare]
#[derive(Debug, Serialize, Clone)]
/// The payload of the [`ActionType::UpdatePlatingNotes`].
#[serde(rename_all = "camelCase")]
pub struct UpdatePlantingNoteActionPayload {
    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,
}

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,
        }
    }
}

#[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,
        }
    }
}