moosicbox_library 0.3.0

MoosicBox Library Music API package
Documentation
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
//! Database operations for library metadata.
//!
//! This module provides database access functions for managing library data including
//! artists, albums, tracks, and their relationships. It handles CRUD operations and
//! complex queries for the library database.

use moosicbox_json_utils::{
    ParseError, ToValueType,
    database::{AsModelResultMapped as _, DatabaseFetchError, ToValue as _},
};
use moosicbox_music_models::{
    ApiSource, AudioFormat, PlaybackQuality, TrackApiSource, TrackSize, id::Id,
};
use switchy_database::{
    DatabaseError, DatabaseValue, Row, boxed,
    profiles::LibraryDatabase,
    query::{
        FilterableQuery, SortDirection, coalesce, identifier, literal, select, where_in,
        where_not_eq,
    },
};
use thiserror::Error;

/// Database model types for library entities.
pub mod models;

use crate::{
    db::models::LibraryConfig,
    models::{LibraryAlbum, LibraryArtist, LibraryTrack},
};

/// Creates or updates a library authentication configuration.
///
/// # Errors
///
/// * If there was a database error
#[allow(clippy::too_many_arguments)]
pub async fn create_library_config(
    db: &LibraryDatabase,
    client_id: &str,
    access_token: &str,
    refresh_token: &str,
    client_name: &str,
    expires_in: u32,
    scope: &str,
    token_type: &str,
    user: &str,
    user_id: u32,
) -> Result<(), DatabaseError> {
    db.upsert("library_config")
        .value("client_id", client_id)
        .value("access_token", access_token)
        .value("refresh_token", refresh_token)
        .value("client_name", client_name)
        .value("expires_in", expires_in)
        .value("scope", scope)
        .value("token_type", token_type)
        .value("user", user)
        .value("user_id", user_id)
        .where_eq("refresh_token", refresh_token)
        .execute(&**db)
        .await?;

    Ok(())
}

/// Deletes a library authentication configuration by refresh token.
///
/// # Errors
///
/// * If there was a database error
pub async fn delete_library_config(
    db: &LibraryDatabase,
    refresh_token: &str,
) -> Result<(), DatabaseError> {
    db.delete("library_config")
        .where_eq("refresh_token", refresh_token)
        .execute(&**db)
        .await?;

    Ok(())
}

/// Errors that can occur when retrieving library configuration.
#[derive(Debug, Error)]
pub enum LibraryConfigError {
    /// Database operation failed.
    #[error(transparent)]
    Database(#[from] DatabaseError),
    /// Failed to parse configuration data.
    #[error(transparent)]
    Parse(#[from] moosicbox_json_utils::ParseError),
    /// No library configurations are available.
    #[error("No configs available")]
    NoConfigsAvailable,
}

/// Retrieves the library authentication configuration.
///
/// # Errors
///
/// * If there was a database error
/// * If there were no configs available
pub async fn get_library_config(
    db: &LibraryDatabase,
) -> Result<Option<LibraryConfig>, LibraryConfigError> {
    let mut configs = db
        .select("library_config")
        .execute(&**db)
        .await?
        .to_value_type()?;

    if configs.is_empty() {
        return Err(LibraryConfigError::NoConfigsAvailable);
    }

    configs.sort_by_key(|a: &LibraryConfig| a.issued_at);

    Ok(configs.first().cloned())
}

/// Retrieves the library access token and refresh token pair.
///
/// # Errors
///
/// * If there was a database error
pub async fn get_library_access_tokens(
    db: &LibraryDatabase,
) -> Result<Option<(String, String)>, LibraryConfigError> {
    Ok(get_library_config(db)
        .await?
        .map(|c| (c.access_token.clone(), c.refresh_token)))
}

/// Retrieves the library access token.
///
/// # Errors
///
/// * If there was a database error
pub async fn get_library_access_token(
    db: &LibraryDatabase,
) -> Result<Option<String>, LibraryConfigError> {
    Ok(get_library_access_tokens(db).await?.map(|c| c.0))
}

/// Retrieves all artists from the library database.
///
/// # Errors
///
/// * If there was a database error
pub async fn get_artists(db: &LibraryDatabase) -> Result<Vec<LibraryArtist>, DatabaseFetchError> {
    Ok(db.select("artists").execute(&**db).await?.to_value_type()?)
}

/// Retrieves all albums from the library database with audio format information.
///
/// # Errors
///
/// * If there was a database error
pub async fn get_albums(db: &LibraryDatabase) -> Result<Vec<LibraryAlbum>, DatabaseFetchError> {
    db.select("albums")
        .distinct()
        .columns(&[
            "albums.*",
            "albums.id as album_id",
            "track_sizes.bit_depth",
            "track_sizes.sample_rate",
            "track_sizes.channels",
            "track_sizes.format",
            "artists.title as artist",
            "tracks.source",
            "artists.api_sources as artist_api_sources",
        ])
        .left_join("tracks", "tracks.album_id=albums.id")
        .left_join("track_sizes", "track_sizes.track_id=tracks.id")
        .join("artists", "artists.id=albums.artist_id")
        .sort("albums.id", SortDirection::Desc)
        .where_or(boxed![
            where_not_eq("track_sizes.format", AudioFormat::Source.as_ref()),
            where_not_eq("tracks.source", TrackApiSource::Local.to_string())
        ])
        .execute(&**db)
        .await?
        .as_model_mapped()
}

/// Retrieves a specific artist by ID from a given API source.
///
/// # Errors
///
/// * If there was a database error
pub async fn get_artist(
    db: &LibraryDatabase,
    api_source: &ApiSource,
    id: &Id,
) -> Result<Option<LibraryArtist>, DatabaseFetchError> {
    Ok(if api_source.is_library() {
        db.select("artists")
            .where_eq("id", id)
            .execute_first(&**db)
            .await?
            .as_ref()
            .to_value_type()?
    } else {
        db.select("artists")
            .join(
                "api_sources",
                "api_sources.entity_type='artists' AND api_sources.entity_id = artists.id",
            )
            .where_eq("api_sources.source", api_source.as_ref())
            .where_eq("api_sources.source_id", id)
            .execute_first(&**db)
            .await?
            .as_ref()
            .to_value_type()?
    })
}

/// Retrieves the artist associated with a specific album ID.
///
/// # Errors
///
/// * If there was a database error
pub async fn get_artist_by_album_id(
    db: &LibraryDatabase,
    id: u64,
) -> Result<Option<LibraryArtist>, DatabaseFetchError> {
    Ok(db
        .select("artists")
        .where_eq("albums.id", id)
        .join("albums", "albums.artist_id = artists.id")
        .execute_first(&**db)
        .await?
        .as_ref()
        .to_value_type()?)
}

/// Retrieves all artists associated with the given album IDs.
///
/// # Errors
///
/// * If there was a database error
pub async fn get_artists_by_album_ids(
    db: &LibraryDatabase,
    album_ids: &[u64],
) -> Result<Vec<LibraryArtist>, DatabaseFetchError> {
    Ok(db
        .select("artists")
        .distinct()
        .join("albums", "albums.artist_id = artists.id")
        .where_in("album.id", album_ids.to_vec())
        .execute(&**db)
        .await?
        .to_value_type()?)
}

/// Retrieves the artist for a specific album.
///
/// # Errors
///
/// * If there was a database error
pub async fn get_album_artist(
    db: &LibraryDatabase,
    album_id: u64,
) -> Result<Option<LibraryArtist>, DatabaseFetchError> {
    Ok(db
        .select("artists")
        .join("albums", "albums.artist_id=artists.id")
        .where_eq("albums.id", album_id)
        .execute_first(&**db)
        .await?
        .map(|x| x.to_value_type())
        .transpose()?)
}

/// Retrieves a specific album by ID from a given API source.
///
/// # Errors
///
/// * If there was a database error
pub async fn get_album(
    db: &LibraryDatabase,
    api_source: &ApiSource,
    id: &Id,
) -> Result<Option<LibraryAlbum>, DatabaseFetchError> {
    Ok(if api_source.is_library() {
        db.select("albums")
            .columns(&[
                "albums.*",
                "artists.title as artist",
                "artists.api_sources as artist_api_sources",
            ])
            .join("artists", "artists.id = albums.artist_id")
            .where_eq("albums.id", id)
            .execute_first(&**db)
            .await?
            .as_ref()
            .to_value_type()?
    } else {
        db.select("albums")
            .columns(&[
                "albums.*",
                "artists.title as artist",
                "artists.api_sources as artist_api_sources",
            ])
            .join("artists", "artists.id = albums.artist_id")
            .join(
                "api_sources",
                "api_sources.entity_type='albums' AND api_sources.entity_id = albums.id",
            )
            .where_eq("api_sources.source", api_source.as_ref())
            .where_eq("api_sources.source_id", id)
            .execute_first(&**db)
            .await?
            .as_ref()
            .to_value_type()?
    })
}

/// Retrieves all tracks for a specific album.
///
/// # Errors
///
/// * If there was a database error
pub async fn get_album_tracks(
    db: &LibraryDatabase,
    album_id: &Id,
) -> Result<Vec<LibraryTrack>, DatabaseFetchError> {
    Ok(db
        .select("tracks")
        .columns(&[
            "tracks.*",
            "albums.title as album",
            "albums.blur as blur",
            "albums.date_released as date_released",
            "albums.date_added as date_added",
            "artists.title as artist",
            "artists.id as artist_id",
            "albums.artwork",
            "track_sizes.format",
            "track_sizes.bytes",
            "track_sizes.bit_depth",
            "track_sizes.audio_bitrate",
            "track_sizes.overall_bitrate",
            "track_sizes.sample_rate",
            "track_sizes.channels",
            "albums.api_sources as album_api_sources",
            "artists.api_sources as artist_api_sources",
        ])
        .where_eq("tracks.album_id", album_id)
        .join("albums", "albums.id=tracks.album_id")
        .join("artists", "artists.id=albums.artist_id")
        .left_join(
            "track_sizes",
            "tracks.id=track_sizes.track_id AND track_sizes.format=tracks.format",
        )
        .sort("number", SortDirection::Asc)
        .execute(&**db)
        .await?
        .to_value_type()?)
}

/// Retrieves all albums for a specific artist.
///
/// # Errors
///
/// * If there was a database error
pub async fn get_artist_albums(
    db: &LibraryDatabase,
    artist_id: &Id,
) -> Result<Vec<LibraryAlbum>, DatabaseFetchError> {
    db.select("albums")
        .distinct()
        .columns(&[
            "albums.*",
            "albums.id as album_id",
            "track_sizes.bit_depth",
            "track_sizes.sample_rate",
            "track_sizes.channels",
            "artists.title as artist",
            "tracks.format",
            "tracks.source",
            "artists.api_sources as artist_api_sources",
        ])
        .left_join("tracks", "tracks.album_id=albums.id")
        .left_join("track_sizes", "track_sizes.track_id=tracks.id")
        .join("artists", "artists.id=albums.artist_id")
        .where_eq("albums.artist_id", artist_id)
        .sort("albums.id", SortDirection::Desc)
        .execute(&**db)
        .await?
        .as_model_mapped()
}

/// Parameters for creating or updating track size information in the database.
///
/// Contains all the audio quality metadata for a track at a specific playback quality level.
#[derive(Debug, Clone)]
pub struct SetTrackSize {
    /// Track ID.
    pub track_id: u64,
    /// Playback quality.
    pub quality: PlaybackQuality,
    /// File size in bytes.
    pub bytes: Option<Option<u64>>,
    /// Audio bit depth.
    pub bit_depth: Option<Option<u8>>,
    /// Audio bitrate.
    pub audio_bitrate: Option<Option<u32>>,
    /// Overall bitrate.
    pub overall_bitrate: Option<Option<u32>>,
    /// Sample rate.
    pub sample_rate: Option<Option<u32>>,
    /// Number of audio channels.
    pub channels: Option<Option<u8>>,
}

/// Sets the audio size information for a single track.
///
/// # Errors
///
/// * If there was a database error
pub async fn set_track_size(
    db: &LibraryDatabase,
    value: SetTrackSize,
) -> Result<Option<TrackSize>, DatabaseFetchError> {
    Ok(set_track_sizes(db, &[value]).await?.first().cloned())
}

/// Sets the audio size information for multiple tracks.
///
/// # Errors
///
/// * If there was a database error
pub async fn set_track_sizes(
    db: &LibraryDatabase,
    values: &[SetTrackSize],
) -> Result<Vec<TrackSize>, DatabaseFetchError> {
    let values = values
        .iter()
        .map(|v| {
            let mut values = vec![
                (
                    "track_id",
                    #[allow(clippy::cast_possible_wrap)]
                    DatabaseValue::Int64(v.track_id as i64),
                ),
                (
                    "format",
                    DatabaseValue::String(v.quality.format.as_ref().to_string()),
                ),
            ];

            if let Some(bytes) = v.bytes {
                values.push((
                    "bytes",
                    #[allow(clippy::cast_possible_wrap)]
                    DatabaseValue::Int64Opt(bytes.map(|x| x as i64)),
                ));
            }
            if let Some(bit_depth) = v.bit_depth {
                values.push((
                    "bit_depth",
                    DatabaseValue::Int64Opt(bit_depth.map(i64::from)),
                ));
            }
            if let Some(audio_bitrate) = v.audio_bitrate {
                values.push((
                    "audio_bitrate",
                    DatabaseValue::Int64Opt(audio_bitrate.map(i64::from)),
                ));
            }
            if let Some(overall_bitrate) = v.overall_bitrate {
                values.push((
                    "overall_bitrate",
                    DatabaseValue::Int64Opt(overall_bitrate.map(i64::from)),
                ));
            }
            if let Some(sample_rate) = v.sample_rate {
                values.push((
                    "sample_rate",
                    DatabaseValue::Int64Opt(sample_rate.map(i64::from)),
                ));
            }
            if let Some(channels) = v.channels {
                values.push(("channels", DatabaseValue::Int64Opt(channels.map(i64::from))));
            }

            values
        })
        .collect::<Vec<_>>();

    Ok(db
        .upsert_multi("track_sizes")
        .unique(boxed![
            identifier("track_id"),
            coalesce(boxed![identifier("format"), literal("''")]),
            coalesce(boxed![identifier("audio_bitrate"), literal("0")]),
            coalesce(boxed![identifier("overall_bitrate"), literal("0")]),
            coalesce(boxed![identifier("bit_depth"), literal("0")]),
            coalesce(boxed![identifier("sample_rate"), literal("0")]),
            coalesce(boxed![identifier("channels"), literal("0")]),
        ])
        .values(values.clone())
        .execute(&**db)
        .await?
        .to_value_type()?)
}

/// Retrieves the file size in bytes for a track at a specific quality level.
///
/// # Errors
///
/// * If there was a database error
pub async fn get_track_size(
    db: &LibraryDatabase,
    id: &Id,
    quality: &PlaybackQuality,
) -> Result<Option<u64>, DatabaseFetchError> {
    Ok(db
        .select("track_sizes")
        .columns(&["bytes"])
        .where_eq("track_id", id.to_string())
        .where_eq("format", quality.format.as_ref())
        .execute_first(&**db)
        .await?
        .and_then(|x| x.columns.first().cloned())
        .map(|(_, value)| value)
        .map(|col| col.to_value_type() as Result<Option<u64>, _>)
        .transpose()?
        .flatten())
}

/// Retrieves a specific track by ID.
///
/// # Errors
///
/// * If there was a database error
pub async fn get_track(
    db: &LibraryDatabase,
    id: &Id,
) -> Result<Option<LibraryTrack>, DatabaseFetchError> {
    Ok(get_tracks(db, Some(&[id.to_owned()]))
        .await?
        .into_iter()
        .next())
}

/// Retrieves tracks by their IDs, or all tracks if no IDs are provided.
///
/// # Errors
///
/// * If there was a database error
pub async fn get_tracks(
    db: &LibraryDatabase,
    ids: Option<&[Id]>,
) -> Result<Vec<LibraryTrack>, DatabaseFetchError> {
    if ids.is_some_and(<[Id]>::is_empty) {
        return Ok(vec![]);
    }

    Ok(db
        .select("tracks")
        .columns(&[
            "tracks.*",
            "albums.title as album",
            "albums.blur as blur",
            "albums.date_released as date_released",
            "albums.date_added as date_added",
            "artists.title as artist",
            "artists.id as artist_id",
            "albums.artwork",
            "track_sizes.format",
            "track_sizes.bytes",
            "track_sizes.bit_depth",
            "track_sizes.audio_bitrate",
            "track_sizes.overall_bitrate",
            "track_sizes.sample_rate",
            "track_sizes.channels",
            "albums.api_sources as album_api_sources",
            "artists.api_sources as artist_api_sources",
        ])
        .filter_if_some(ids.map(|ids| where_in("tracks.id", ids.to_vec())))
        .join("albums", "albums.id=tracks.album_id")
        .join("artists", "artists.id=albums.artist_id")
        .left_join(
            "track_sizes",
            "tracks.id=track_sizes.track_id AND track_sizes.format=tracks.format",
        )
        .execute(&**db)
        .await?
        .to_value_type()?)
}

/// Deletes a single track from the database by its ID.
///
/// # Errors
///
/// * If there was a database error
pub async fn delete_track(
    db: &LibraryDatabase,
    id: u64,
) -> Result<Option<LibraryTrack>, DatabaseFetchError> {
    Ok(delete_tracks(db, Some(&vec![id])).await?.into_iter().next())
}

/// Deletes multiple tracks from the database by their IDs.
///
/// # Errors
///
/// * If there was a database error
pub async fn delete_tracks(
    db: &LibraryDatabase,
    ids: Option<&Vec<u64>>,
) -> Result<Vec<LibraryTrack>, DatabaseFetchError> {
    if ids.is_some_and(Vec::is_empty) {
        return Ok(vec![]);
    }

    Ok(db
        .delete("tracks")
        .filter_if_some(ids.map(|ids| where_in("id", ids.clone())))
        .execute(&**db)
        .await?
        .to_value_type()?)
}

/// Deletes track size information for a single track.
///
/// # Errors
///
/// * If there was a database error
pub async fn delete_track_size_by_track_id(
    db: &LibraryDatabase,
    id: u64,
) -> Result<Option<TrackSize>, DatabaseFetchError> {
    Ok(delete_track_sizes_by_track_id(db, Some(&vec![id]))
        .await?
        .into_iter()
        .next())
}

/// Deletes track size information for multiple tracks.
///
/// # Errors
///
/// * If there was a database error
pub async fn delete_track_sizes_by_track_id(
    db: &LibraryDatabase,
    ids: Option<&Vec<u64>>,
) -> Result<Vec<TrackSize>, DatabaseFetchError> {
    if ids.is_some_and(Vec::is_empty) {
        return Ok(vec![]);
    }

    Ok(db
        .delete("track_sizes")
        .filter_if_some(ids.map(|ids| where_in("track_id", ids.clone())))
        .execute(&**db)
        .await?
        .to_value_type()?)
}

/// Adds a single artist to the database and returns the created/existing artist.
///
/// # Errors
///
/// * If there was a database error
pub async fn add_artist_and_get_artist(
    db: &LibraryDatabase,
    artist: LibraryArtist,
) -> Result<LibraryArtist, DatabaseFetchError> {
    Ok(add_artists_and_get_artists(db, vec![artist]).await?[0].clone())
}

/// Adds a single artist from a field map to the database and returns the created/existing artist.
///
/// # Errors
///
/// * If there was a database error
pub async fn add_artist_map_and_get_artist(
    db: &LibraryDatabase,
    artist: Vec<(&str, DatabaseValue)>,
) -> Result<LibraryArtist, DatabaseFetchError> {
    Ok(add_artist_maps_and_get_artists(db, vec![artist]).await?[0].clone())
}

/// Adds multiple artists to the database and returns the created/existing artists.
///
/// # Errors
///
/// * If there was a database error
pub async fn add_artists_and_get_artists(
    db: &LibraryDatabase,
    artists: Vec<LibraryArtist>,
) -> Result<Vec<LibraryArtist>, DatabaseFetchError> {
    add_artist_maps_and_get_artists(
        db,
        artists
            .into_iter()
            .map(|artist| {
                vec![
                    ("title", DatabaseValue::String(artist.title)),
                    ("cover", DatabaseValue::StringOpt(artist.cover)),
                ]
            })
            .collect(),
    )
    .await
}

/// Adds multiple artists from field maps to the database and returns the created/existing artists.
///
/// # Errors
///
/// * If there was a database error
pub async fn add_artist_maps_and_get_artists(
    db: &LibraryDatabase,
    artists: Vec<Vec<(&str, DatabaseValue)>>,
) -> Result<Vec<LibraryArtist>, DatabaseFetchError> {
    let mut results = vec![];

    for artist in artists {
        let title = artist
            .iter()
            .find(|(key, _)| *key == "title")
            .and_then(|(_, value)| value.as_str().map(ToString::to_string))
            .ok_or(DatabaseFetchError::InvalidRequest)?;

        let row: LibraryArtist = db
            .upsert("artists")
            .where_eq("title", title)
            .values(artist.into_iter().collect::<Vec<_>>())
            .execute_first(&**db)
            .await?
            .to_value_type()?;

        results.push(row);
    }

    Ok(results)
}

/// Adds multiple albums to the database without returning them.
///
/// # Errors
///
/// * If there was a database error
pub async fn add_albums(
    db: &LibraryDatabase,
    albums: Vec<LibraryAlbum>,
) -> Result<Vec<LibraryAlbum>, DatabaseFetchError> {
    let mut data: Vec<LibraryAlbum> = Vec::new();

    for album in albums {
        data.push(
            db.upsert("albums")
                .where_eq("artist_id", album.artist_id)
                .where_eq("title", album.title.clone())
                .where_eq("directory", album.directory.clone())
                .value("artist_id", album.artist_id)
                .value("title", album.title)
                .value("directory", album.directory)
                .value("date_released", album.date_released)
                .value("artwork", album.artwork)
                .execute_first(&**db)
                .await?
                .to_value_type()?,
        );
    }

    Ok(data)
}

/// Adds a single album to the database and returns the created/existing album.
///
/// # Errors
///
/// * If there was a database error
pub async fn add_album_and_get_album(
    db: &LibraryDatabase,
    album: LibraryAlbum,
) -> Result<LibraryAlbum, DatabaseFetchError> {
    Ok(add_albums_and_get_albums(db, vec![album]).await?[0].clone())
}

/// Adds a single album from a field map to the database and returns the created/existing album.
///
/// # Errors
///
/// * If there was a database error
pub async fn add_album_map_and_get_album(
    db: &LibraryDatabase,
    album: Vec<(&str, DatabaseValue)>,
) -> Result<LibraryAlbum, DatabaseFetchError> {
    Ok(add_album_maps_and_get_albums(db, vec![album]).await?[0].clone())
}

/// Adds multiple albums to the database and returns the created/existing albums.
///
/// # Errors
///
/// * If there was a database error
pub async fn add_albums_and_get_albums(
    db: &LibraryDatabase,
    albums: Vec<LibraryAlbum>,
) -> Result<Vec<LibraryAlbum>, DatabaseFetchError> {
    add_album_maps_and_get_albums(
        db,
        albums
            .into_iter()
            .map(|album| {
                vec![
                    (
                        "artist_id",
                        #[allow(clippy::cast_possible_wrap)]
                        DatabaseValue::Int64(album.artist_id as i64),
                    ),
                    ("title", DatabaseValue::String(album.title)),
                    (
                        "date_released",
                        DatabaseValue::StringOpt(album.date_released),
                    ),
                    ("artwork", DatabaseValue::StringOpt(album.artwork)),
                    ("directory", DatabaseValue::StringOpt(album.directory)),
                ]
            })
            .collect(),
    )
    .await
}

/// Adds multiple albums from field maps to the database and returns the created/existing albums.
///
/// # Errors
///
/// * If there was a database error
pub async fn add_album_maps_and_get_albums(
    db: &LibraryDatabase,
    albums: Vec<Vec<(&str, DatabaseValue)>>,
) -> Result<Vec<LibraryAlbum>, DatabaseFetchError> {
    let mut values = vec![];

    for album in albums {
        if !album.iter().any(|(x, _)| *x == "artist_id")
            || !album.iter().any(|(x, _)| *x == "title")
        {
            return Err(DatabaseFetchError::InvalidRequest);
        }

        let mut album_values = album.into_iter().collect::<Vec<_>>();
        album_values.sort_by(|a, b| a.0.cmp(b.0));
        values.push(album_values);
    }

    Ok(db
        .upsert_multi("albums")
        .unique(boxed![identifier("artist_id"), identifier("title")])
        .values(values)
        .execute(&**db)
        .await?
        .to_value_type()?)
}

/// Parameters for inserting a new track into the database.
///
/// Contains the track data along with album association and optional file path.
#[derive(Debug, Clone, Default)]
pub struct InsertTrack {
    /// Track to insert.
    pub track: LibraryTrack,
    /// Album ID.
    pub album_id: u64,
    /// File path.
    pub file: Option<String>,
}

/// Adds multiple tracks to the database and returns the created/existing tracks.
///
/// # Errors
///
/// * If there was a database error
pub async fn add_tracks(
    db: &LibraryDatabase,
    tracks: Vec<InsertTrack>,
) -> Result<Vec<LibraryTrack>, DatabaseFetchError> {
    let values = tracks
        .iter()
        .map(|insert| {
            let mut values = vec![
                (
                    "number",
                    DatabaseValue::Int64(i64::from(insert.track.number)),
                ),
                ("duration", DatabaseValue::Real64(insert.track.duration)),
                (
                    "album_id",
                    #[allow(clippy::cast_possible_wrap)]
                    DatabaseValue::Int64(insert.album_id as i64),
                ),
                ("title", DatabaseValue::String(insert.track.title.clone())),
                (
                    "format",
                    DatabaseValue::String(
                        insert.track.format.unwrap_or_default().as_ref().to_string(),
                    ),
                ),
                (
                    "source",
                    DatabaseValue::String(insert.track.source.to_string()),
                ),
            ];

            if let Some(file) = &insert.file {
                values.push(("file", DatabaseValue::String(file.clone())));
            }

            values
        })
        .collect::<Vec<_>>();

    Ok(db
        .upsert_multi("tracks")
        .unique(boxed![
            coalesce(boxed![identifier("file"), literal("''")]),
            identifier("album_id"),
            identifier("title"),
            identifier("duration"),
            identifier("number"),
            coalesce(boxed![identifier("format"), literal("''")]),
            identifier("source"),
        ])
        .values(values)
        .execute(&**db)
        .await?
        .to_value_type()?)
}

/// Parameters for inserting an API source mapping into the database.
///
/// Links a library entity (artist, album, or track) to an external API source.
#[derive(Debug, Clone, Default)]
pub struct InsertApiSource {
    /// Entity type.
    pub entity_type: String,
    /// Entity ID.
    pub entity_id: u64,
    /// API source name.
    pub source: String,
    /// API source ID.
    pub source_id: String,
}

/// API source mapping between library entities and external API sources.
///
/// Represents the relationship between a local library entity and its corresponding
/// identifier in an external music API.
pub struct ApiSourceMapping {
    /// Entity type (e.g., "artists", "albums", "tracks").
    pub entity_type: String,
    /// Entity ID.
    pub entity_id: u64,
    /// API source name.
    pub source: String,
    /// API source ID.
    pub source_id: String,
}

impl ToValueType<ApiSourceMapping> for &switchy_database::Row {
    fn to_value_type(self) -> Result<ApiSourceMapping, ParseError> {
        Ok(ApiSourceMapping {
            entity_type: self.to_value("entity_type")?,
            entity_id: self.to_value("entity_id")?,
            source: self.to_value("source")?,
            source_id: self.to_value("source_id")?,
        })
    }
}

/// Adds multiple API source mappings to the database.
///
/// # Errors
///
/// * If there was a database error
pub async fn add_api_sources(
    db: &LibraryDatabase,
    api_sources: Vec<InsertApiSource>,
) -> Result<Vec<ApiSourceMapping>, DatabaseFetchError> {
    let values = api_sources
        .iter()
        .map(|insert| {
            vec![
                (
                    "entity_type",
                    DatabaseValue::String(insert.entity_type.clone()),
                ),
                ("entity_id", DatabaseValue::UInt64(insert.entity_id)),
                ("source", DatabaseValue::String(insert.source.clone())),
                ("source_id", DatabaseValue::String(insert.source_id.clone())),
            ]
        })
        .collect::<Vec<_>>();

    Ok(db
        .upsert_multi("api_sources")
        .unique(boxed![
            identifier("entity_type"),
            identifier("entity_id"),
            identifier("source"),
        ])
        .values(values)
        .execute(&**db)
        .await?
        .to_value_type()?)
}

/// Parameters for updating an existing API source mapping.
///
/// Used to modify the external API source identifier for a library entity.
#[derive(Debug, Clone, Default)]
pub struct UpdateApiSource {
    /// Entity ID.
    pub entity_id: u64,
    /// API source name.
    pub source: String,
    /// API source ID.
    pub source_id: String,
}

/// Updates API source mappings for a table by regenerating the JSON array.
///
/// # Errors
///
/// * If there was a database error
pub async fn update_api_sources(
    db: &LibraryDatabase,
    table: &str,
) -> Result<Vec<Row>, DatabaseFetchError> {
    Ok(db
        .update(table)
        .value(
            "api_sources",
            Box::new(
                select("api_sources")
                    .columns(&["\
                    json_group_array(
                        json_object(
                           'id', api_sources.source_id,
                           'source', api_sources.source
                        )
                    )\
                    "])
                    .where_eq("api_sources.entity_type", table)
                    .where_eq("api_sources.entity_id", identifier(&format!("{table}.id"))),
            ) as Box<dyn switchy_database::query::Expression>,
        )
        .execute(&**db)
        .await?)
}