commit 6f451cc15082223f16b7f12c0a8127406dae6220
Author: lbihannic <lucas.bihannic@inrae.fr>
Date:   Thu Apr 24 14:10:43 2025 +0200

    add SQLite persistance support

diff --git a/GES_PAC/App.xaml.cs b/GES_PAC/App.xaml.cs
index 2f4b450..6e9594d 100644
--- a/GES_PAC/App.xaml.cs
+++ b/GES_PAC/App.xaml.cs
@@ -1,15 +1,67 @@
+using GES_PAC.Model;
+using GES_PAC.ViewModel;
+using Microsoft.EntityFrameworkCore;
+
 namespace GES_PAC
 {
     public partial class App : Application
     {
+        public static AppDbContext Db { get; private set; }
         public App()
         {
             InitializeComponent();
+
+            //var dbPath = Path.Combine(FileSystem.AppDataDirectory, "GES_PAC.db3");
+            //if (File.Exists(dbPath))
+            //    File.Delete(dbPath);
+
+
+            Db = new AppDbContext();
+            Db.Database.EnsureCreated();
+
+            _ = InitSmartEnumsAsync();
+            //_ = InitDb();
         }
 
         protected override Window CreateWindow(IActivationState? activationState)
         {
             return new Window(new AppShell());
         }
+
+        public static async Task InitSmartEnumsAsync()
+        {
+            foreach (var phase in PhaseCalibration.All)
+            {
+                if (!await Db.PhaseCalibration.AnyAsync(p => p.Value == phase.Value))
+                    Db.PhaseCalibration.Add(phase);
+            }
+
+            foreach (var type in TypeCalibration.All)
+            {
+                if (!await Db.TypeCalibration.AnyAsync(t => t.Value == type.Value))
+                    Db.TypeCalibration.Add(type);
+            }
+       
+            foreach (var comportement in TypeComportement.All)
+            {
+                if (!await Db.TypeComportement.AnyAsync(c => c.Value == comportement.Value))
+                    Db.TypeComportement.Add(comportement);
+            }
+
+            await Db.SaveChangesAsync();
+        }
+
+
+
+        private async Task InitDb()
+        {
+            //ONLY FOR TESTS
+            var place = new Lieu("Elevage Langlade", "INRAE", "Mouton");
+            var person = new Personne("Bond", "James", "jamesbond@inrae.fr");
+            await Db.Personne.AddAsync(person);
+            await Db.Lieu.AddAsync(place);
+            await Db.SaveChangesAsync();
+            //ONLY FOR TESTS
+        }
     }
 }
\ No newline at end of file
diff --git a/GES_PAC/GES_PAC.csproj b/GES_PAC/GES_PAC.csproj
index bab2a21..b70aa61 100644
--- a/GES_PAC/GES_PAC.csproj
+++ b/GES_PAC/GES_PAC.csproj
@@ -20,6 +20,7 @@
 		<SingleProject>true</SingleProject>
 		<ImplicitUsings>enable</ImplicitUsings>
 		<Nullable>enable</Nullable>
+		<GenerateSatelliteAssemblies>false</GenerateSatelliteAssemblies>
 
 		<!-- Display name -->
 		<ApplicationTitle>GES_PAC</ApplicationTitle>
@@ -70,8 +71,10 @@
 
 	<ItemGroup>
 		<PackageReference Include="CommunityToolkit.Maui" Version="9.1.1" />
+		<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.4" />
 		<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
 		<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" />
+		<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
 	</ItemGroup>
 
 	<ItemGroup>
diff --git a/GES_PAC/Model/Calibration.cs b/GES_PAC/Model/Calibration.cs
index fe4286d..030b5d6 100644
--- a/GES_PAC/Model/Calibration.cs
+++ b/GES_PAC/Model/Calibration.cs
@@ -1,22 +1,34 @@
-
+using SQLite;
+using System.ComponentModel.DataAnnotations.Schema;
 
 namespace GES_PAC.Model
 {
     public class Calibration
     {
-        public long Id { get; set; }
+        #region Propri�t�s
+        [PrimaryKey, AutoIncrement]
+        public int Id { get; set; }
+        public int PhaseCalibrationId { get; set; }
+        [ForeignKey("PhaseCalibrationId")]
         public PhaseCalibration Phase { get; set; }
-        public List<MesureCalibration> Mesures { get; set; }
-        
+        public List<MesureCalibration> Mesures { get; set; } = [];
+        public int JourneeId { get; set; }
+        [ForeignKey("JourneeId")]
+        public Journee Journee { get; set; }
+        #endregion
+
+        #region Constructeurs
+        public Calibration() { }
         public Calibration(PhaseCalibration Phase, MesureCalibration Mesure)
         {
             this.Phase = Phase;
             Mesures = [];
             Mesures.Add(Mesure);
         }
-
+        #endregion
         public void AddMesure(MesureCalibration mesure)
         {
+            mesure.Calibration = this;
             var index = Mesures.FindIndex(m => m.Type == mesure.Type);
             if (index != -1)
             {
diff --git a/GES_PAC/Model/Comportement.cs b/GES_PAC/Model/Comportement.cs
index 7881226..31f5368 100644
--- a/GES_PAC/Model/Comportement.cs
+++ b/GES_PAC/Model/Comportement.cs
@@ -1,18 +1,32 @@
 
+using SQLite;
+using System.ComponentModel.DataAnnotations.Schema;
+
 namespace GES_PAC.Model
 {
     public class Comportement
     {
-        public long Id { get; set; }
+        #region Propri�t�s
+        [PrimaryKey, AutoIncrement]
+        public int Id { get; set; }
+        public int SerieAnimalId { get; set; }
+        [ForeignKey("SerieAnimalId")]
+        public SerieAnimal SerieAnimal { get; set; }
         public DateTime Time { get; set; }
+        public int TypeComportementId { get; set; }
+        [ForeignKey("TypeComportementId")]
         public TypeComportement Type { get; set; }
         public string? Commentaire { get; set; }
+        #endregion
 
+        #region Constructeurs
+        public Comportement() { }
         public Comportement(DateTime Time, TypeComportement Type, string Commentaire)
         {
             this.Time = Time;
             this.Type = Type;
             this.Commentaire = Commentaire;
         }
+        #endregion
     }
 }
diff --git a/GES_PAC/Model/IMesureBase.cs b/GES_PAC/Model/IMesureBase.cs
new file mode 100644
index 0000000..d2b0742
--- /dev/null
+++ b/GES_PAC/Model/IMesureBase.cs
@@ -0,0 +1,15 @@
+﻿using SQLite;
+
+namespace GES_PAC.Model
+{
+    internal interface IMesureBase
+    {
+        #region Propriétés
+        public int Id { get; set; }
+        public DateTime Time { get; set; }
+        public double Conc_O2 { get; set; }
+        public double Conc_CO2 { get; set; }
+        public double Conc_CH4 { get; set; }
+        #endregion
+    }
+}
diff --git a/GES_PAC/Model/Journee.cs b/GES_PAC/Model/Journee.cs
index a0ccf5f..36547f4 100644
--- a/GES_PAC/Model/Journee.cs
+++ b/GES_PAC/Model/Journee.cs
@@ -1,18 +1,27 @@
+using SQLite;
+using System.ComponentModel.DataAnnotations.Schema;
+
 namespace GES_PAC.Model
 {
     public class Journee
     {
         #region Propriétés
+        [PrimaryKey, AutoIncrement]
+        public int Id { get; set; }
+        public int PersonneId { get; set; }
+        [ForeignKey("PersonneId")]
         public Personne Responsable { get; set; }
+        public int LieuId { get; set; }
+        [ForeignKey("LieuId")]
         public Lieu Lieu { get; set; }
-        public long Id { get; set; }
         public DateTime Date { get; set; }
         public string Regime { get; set; }
-        public List<Serie> Series { get; set; }
-        public List<Calibration> Calibrations { get; set; }
+        public List<Serie> Series { get; set; } = [];
+        public List<Calibration> Calibrations { get; set; } = [];
         #endregion
 
         #region Constructeurs
+        public Journee(){}
         public Journee(DateTime Date, Personne Responsable, Lieu Lieu, string Regime)
         {
             this.Responsable = Responsable;
@@ -25,51 +34,29 @@ namespace GES_PAC.Model
         #endregion
 
         #region Méthodes
-        public bool AddCalibration(MesureCalibration mesureCalibration, PhaseCalibration phaseCalibration)
-        {
-            var existing = Calibrations.FirstOrDefault(c => c.Phase == phaseCalibration);
-
-            if (existing == null)
-            {
-                Calibrations.Add(new Calibration(phaseCalibration, mesureCalibration));
-                return false;
-            }
-
-            existing.AddMesure(mesureCalibration);
-            return existing.IsComplete();
-        }
 
         public void AddSet(Serie set)
         {
+            set.Journee = this;
             Series.Add(set);
         }
-
-        public PhaseCalibration GetCurrentPhase()
+        public List<TypeCalibration> GetMissingCalibrationByPhase(PhaseCalibration phase)
         {
-            if (Series.Count == 0)
-            {
-                return PhaseCalibration.DEBUT;
-            }
-            return PhaseCalibration.FIN;
-        }
+            if (phase == null)
+                return TypeCalibration.All;
 
-        public Calibration GetCurrentCalibration()
-        {
-            return Calibrations.FirstOrDefault(c => c.Phase == GetCurrentPhase());
-        }
+            var calibPhase = Calibrations.FirstOrDefault(c => c.Phase == phase);
 
-        public List<TypeCalibration> GetCurrentMissingCalibration()
-        {
-            if (Calibrations.Count == 0 || Calibrations.Last().IsComplete())
-            {
+            if (calibPhase == null || calibPhase.IsComplete())
                 return TypeCalibration.All;
-            }
-            return TypeCalibration.All.Except(Calibrations.Last().GetTypesDone()).ToList();
+
+            return TypeCalibration.All.Except(calibPhase.GetTypesDone()).ToList();
         }
-        
-        public Serie GetCurrentSet()
+
+
+        public Serie? GetCurrentSet()
         {
-            return Series.LastOrDefault();
+            return Series?.LastOrDefault();
         }
         public bool HasAnySet()
         {
@@ -99,15 +86,21 @@ namespace GES_PAC.Model
         }
         internal Calibration GetDebutCalibration()
         {
-            return Calibrations.FirstOrDefault(c => c.Phase == PhaseCalibration.DEBUT);
+            return Calibrations.FirstOrDefault(c => c.Phase?.Value == PhaseCalibration.DEBUT.Value);
         }
         internal bool IsCalibrationComplete(PhaseCalibration pc)
         {
-            return Calibrations.FirstOrDefault(c => c.Phase == pc)?.IsComplete() ?? false;
+            return Calibrations?.FirstOrDefault(c => c.Phase == pc)?.IsComplete() ?? false;
         }
         internal bool IsComplete()
         {
-            return Calibrations.FirstOrDefault(c => c.Phase == PhaseCalibration.FIN)?.IsComplete() ?? false;
+            return Calibrations?.FirstOrDefault(c => c.Phase == PhaseCalibration.FIN)?.IsComplete() ?? false;
+        }
+
+        internal void AddCalibration(Calibration calibration)
+        {
+            calibration.Journee = this;
+            Calibrations.Add(calibration);
         }
         #endregion
     }
diff --git a/GES_PAC/Model/Lieu.cs b/GES_PAC/Model/Lieu.cs
index 25a7b2f..1116852 100644
--- a/GES_PAC/Model/Lieu.cs
+++ b/GES_PAC/Model/Lieu.cs
@@ -1,19 +1,27 @@
 
+using SQLite;
+
 namespace GES_PAC.Model
 {
     public class Lieu
     {
-        public long Id { get; set; }
+        #region Propri�t�s
+        [PrimaryKey, AutoIncrement]
+        public int Id { get; set; }
         public string Nom { get; set; }
         public string Client { get; set; }
         public string Espece { get; set; }
+        #endregion
 
+        #region Constructeurs
+        public Lieu() { }
         public Lieu(string nom, string client, string espece)
         {
             Nom = nom;
             Client = client;
             Espece = espece;
         }
+        #endregion
 
         public override string ToString()
         {
diff --git a/GES_PAC/Model/Mesure.cs b/GES_PAC/Model/Mesure.cs
index b96c4f3..5f87a27 100644
--- a/GES_PAC/Model/Mesure.cs
+++ b/GES_PAC/Model/Mesure.cs
@@ -1,14 +1,25 @@
 
+using SQLite;
+using System.ComponentModel.DataAnnotations.Schema;
+
 namespace GES_PAC.Model
 {
-    public class Mesure
+    public class Mesure : IMesureBase
     {
-        public long Id { get; set; }
+        #region Propri�t�s
+        [PrimaryKey, AutoIncrement]
+        public int Id { get; set; }
         public DateTime Time { get; set; }
         public double Conc_O2 { get; set; }
         public double Conc_CO2 { get; set; }
         public double Conc_CH4 { get; set; }
+        public int SerieAnimalId { get; set; }
+        [ForeignKey("SerieAnimalId")]
+        public SerieAnimal SerieAnimal { get; set; }
+        #endregion
 
+        #region Constructeurs
+        public Mesure() { }
         public Mesure(DateTime Time, double Conc_O2, double Conc_CO2, double Conc_CH4)
         {
             this.Time = Time;
@@ -16,5 +27,6 @@ namespace GES_PAC.Model
             this.Conc_CO2 = Conc_CO2;
             this.Conc_CH4 = Conc_CH4;
         }
+        #endregion
     }
 }
diff --git a/GES_PAC/Model/MesureCalibration.cs b/GES_PAC/Model/MesureCalibration.cs
index bc2dead..e2a276c 100644
--- a/GES_PAC/Model/MesureCalibration.cs
+++ b/GES_PAC/Model/MesureCalibration.cs
@@ -1,18 +1,41 @@
+using SQLite;
+using System.ComponentModel.DataAnnotations.Schema;
 
 namespace GES_PAC.Model
 {
-    public class MesureCalibration : Mesure
+    public class MesureCalibration : IMesureBase
     {
+        #region Propri�t�s
+        [PrimaryKey, AutoIncrement]
+        public int Id { get; set; }
+        public DateTime Time { get; set; }
+        public double Conc_O2 { get; set; }
+        public double Conc_CO2 { get; set; }
+        public double Conc_CH4 { get; set; }
+        public int TypeCalibrationId { get; set; }
+        [ForeignKey("TypeCalibrationId")]
         public TypeCalibration Type { get; set; }
-        public string RefBouteille { get; set; }
+        public string? RefBouteille { get; set; }
+        public int CalibrationId { get; set; }
+        [ForeignKey("CalibrationId")]
+        public Calibration Calibration { get; set; }
 
-        public MesureCalibration(DateTime Time, double Conc_O2, double Conc_CO2, double Conc_CH4, TypeCalibration Type, string RefBouteille) : base(Time, Conc_O2, Conc_CO2, Conc_CH4)
+        #endregion
+
+        #region Constructeurs
+        public MesureCalibration() { }
+        public MesureCalibration(DateTime Time, double Conc_O2, double Conc_CO2, double Conc_CH4, TypeCalibration Type, string RefBouteille)
         {
+            this.Time = Time;
+            this.Conc_O2 = Conc_O2;
+            this.Conc_CO2 = Conc_CO2;
+            this.Conc_CH4 = Conc_CH4;
             this.Type = Type;
             if (Type.Value != 0)
             {
                 this.RefBouteille = RefBouteille;
             }
         }
+        #endregion
     }
 }
diff --git a/GES_PAC/Model/Personne.cs b/GES_PAC/Model/Personne.cs
index a80c456..92dceac 100644
--- a/GES_PAC/Model/Personne.cs
+++ b/GES_PAC/Model/Personne.cs
@@ -1,14 +1,19 @@
 
+using SQLite;
+
 namespace GES_PAC.Model
 {
     public class Personne
     {
-        public long Id { get; set; }
+        #region Propri�t�s
+        [PrimaryKey, AutoIncrement]
+        public int Id { get; set; }
         public string? Nom { get; set; }
         public string? Prenom { get; set; }
         public string? Email { get; set; }
+        #endregion
 
-
+        #region Constructeurs
         public Personne() { }
         public Personne(string Nom, string Prenom, string Email)
         {
@@ -16,6 +21,7 @@ namespace GES_PAC.Model
             this.Prenom = Prenom;
             this.Email = Email;
         }
+        #endregion
         public override string ToString()
         {
             return $"{Nom} {Prenom}";
diff --git a/GES_PAC/Model/PhaseCalibration.cs b/GES_PAC/Model/PhaseCalibration.cs
index 98ec2ff..099d11b 100644
--- a/GES_PAC/Model/PhaseCalibration.cs
+++ b/GES_PAC/Model/PhaseCalibration.cs
@@ -1,15 +1,24 @@
+using SQLite;
+
 namespace GES_PAC.Model
 {
     public class PhaseCalibration
     {
-        public string Name { get; }
-        public int Value { get; }
+        #region Propri�t�s
+        [PrimaryKey, AutoIncrement]
+        public int Id { get; set; }
+        public string Name { get; set; }
+        public int Value { get; set; }
+        #endregion
 
+        #region Constructeurs
+        public PhaseCalibration() { }
         private PhaseCalibration(string name, int value)
         {
             Name = name;
             Value = value;
         }
+        #endregion
 
         public static readonly PhaseCalibration DEBUT = new("d�but", 1);
         public static readonly PhaseCalibration FIN = new("fin", 2);
diff --git a/GES_PAC/Model/Serie.cs b/GES_PAC/Model/Serie.cs
index cb83912..2e2e1a8 100644
--- a/GES_PAC/Model/Serie.cs
+++ b/GES_PAC/Model/Serie.cs
@@ -1,12 +1,16 @@
-
-using System.Collections.ObjectModel;
+using SQLite;
+using System.ComponentModel.DataAnnotations.Schema;
 
 namespace GES_PAC.Model
 {
     public class Serie
     {
         #region Propriétés
-        public long Id { get; set; }
+        [PrimaryKey, AutoIncrement]
+        public int Id { get; set; }
+        public int JourneeId { get; set; }
+        [ForeignKey("JourneeId")]
+        public Journee Journee { get; set; }
         public DateTime Time { get; set; }
         public DateTime MiseAJeun { get; set; }
         public double Temperature { get; set; }
@@ -16,6 +20,7 @@ namespace GES_PAC.Model
         #endregion
 
         #region Constructeurs
+        public Serie() { }
         public Serie(DateTime Time, DateTime MiseAJeun, double Temperature, double Humidite, double Pression)
         {
             this.Time = Time;
@@ -30,6 +35,7 @@ namespace GES_PAC.Model
         #region Méthodes
         public void AddSerieAnimal(SerieAnimal serieAnimal)
         {
+            serieAnimal.Serie = this;
             var existing = SeriesAnimales.FirstOrDefault(s => s.NumeroBoite == serieAnimal.NumeroBoite);
             if (existing != null)
                 SeriesAnimales.Remove(existing);
diff --git a/GES_PAC/Model/SerieAnimal.cs b/GES_PAC/Model/SerieAnimal.cs
index 3723660..4b55c87 100644
--- a/GES_PAC/Model/SerieAnimal.cs
+++ b/GES_PAC/Model/SerieAnimal.cs
@@ -1,19 +1,28 @@
 ﻿
-using Microsoft.Maui.Layouts;
+using SQLite;
+using System.ComponentModel.DataAnnotations.Schema;
 
 namespace GES_PAC.Model
 {
     public class SerieAnimal
     {
-        public long Id { get; set; }
+        #region Propriétés
+        [PrimaryKey, AutoIncrement]
+        public int Id { get; set; }
         public int NumeroBoite { get; set; }
         public double Poids { get; set; }
         public DateTime DatePesee { get; set; }
         public string RFID { get; set; }
         public List<Mesure> Mesures { get; set; }
         public List<Comportement> Comportements { get; set; }
+        public int SerieId { get; set; }
+        [ForeignKey("SerieId")]
+        public Serie Serie { get; set; }
         public bool IsOut { get; set; } = false;
+        #endregion
 
+        #region Constructeurs
+        public SerieAnimal() { }
         public SerieAnimal(int numeroBoite, double poids, DateTime datePesee, string rFID)
         {
             NumeroBoite = numeroBoite;
@@ -23,6 +32,7 @@ namespace GES_PAC.Model
             Mesures = [];
             Comportements = [];
         }
+        #endregion
 
         public int GetMeasureCount()
         {
@@ -31,6 +41,7 @@ namespace GES_PAC.Model
 
         internal void AddMeasure(Mesure newMeasure)
         {
+            newMeasure.SerieAnimal = this;
             Mesures.Add(newMeasure);
         }
 
@@ -45,6 +56,7 @@ namespace GES_PAC.Model
 
         public void AddBehaviour(Comportement newBehaviour)
         {
+            newBehaviour.SerieAnimal = this;
             Comportements.Add(newBehaviour);
         }
 
diff --git a/GES_PAC/Model/TypeCalibration.cs b/GES_PAC/Model/TypeCalibration.cs
index 20142bc..091f7b4 100644
--- a/GES_PAC/Model/TypeCalibration.cs
+++ b/GES_PAC/Model/TypeCalibration.cs
@@ -1,21 +1,30 @@
+using SQLite;
+
 namespace GES_PAC.Model
 {
     public class TypeCalibration
     {
-        public string Name { get; }
-        public int Value { get; }
+        #region Propri�t�s
+        [PrimaryKey, AutoIncrement]
+        public int Id { get; set; }
+        public string Name { get; set; }
+        public int Value { get; set; }
+        #endregion
 
-        private TypeCalibration(string name, int value) // a passer en public quand integration webservice
+        #region Constructeurs
+        public TypeCalibration() { }
+        private TypeCalibration(string name, int value)
         {
             Name = name;
             Value = value;
         }
+        #endregion
 
-        public static readonly TypeCalibration Air = new("AIR", 0);
-        public static readonly TypeCalibration Methane = new("METHANE", 1);
-        public static readonly TypeCalibration Melange = new("MELANGE", 2);
+        public static readonly TypeCalibration AIR = new("Air", 0);
+        public static readonly TypeCalibration METHANE = new("M�thane", 1);
+        public static readonly TypeCalibration MELANGE = new("M�lange", 2);
 
-        public static readonly List<TypeCalibration> All = new() { Air, Methane, Melange };
+        public static readonly List<TypeCalibration> All = new() { AIR, METHANE, MELANGE };
 
         public override string ToString() => Name;
     }
diff --git a/GES_PAC/Model/TypeComportement.cs b/GES_PAC/Model/TypeComportement.cs
index 0ea040c..bee86ac 100644
--- a/GES_PAC/Model/TypeComportement.cs
+++ b/GES_PAC/Model/TypeComportement.cs
@@ -1,16 +1,25 @@
 
+using SQLite;
+
 namespace GES_PAC.Model
 {
     public class TypeComportement
     {
-        public string Name { get; }
-        public int Value { get; }
+        #region Propri�t�s
+        [PrimaryKey, AutoIncrement]
+        public int Id { get; set; }
+        public string Name { get; set; }
+        public int Value { get; set; }
+        #endregion
 
+        #region Constructeurs
+        public TypeComportement() { }
         private TypeComportement(string name, int value)
         {
             Name = name;
             Value = value;
         }
+        #endregion
 
         public static readonly TypeComportement IMMOBILITE = new("Immobilit�", 1);
         public static readonly TypeComportement DEPLACEMENT_AVANT_ARRIERE = new("D�placement avant/arri�re", 2);
diff --git a/GES_PAC/Platforms/Android/AndroidFileHelper.cs b/GES_PAC/Platforms/Android/AndroidFileHelper.cs
index d2aa10b..2d4d1bd 100644
--- a/GES_PAC/Platforms/Android/AndroidFileHelper.cs
+++ b/GES_PAC/Platforms/Android/AndroidFileHelper.cs
@@ -11,7 +11,7 @@ public static class AndroidFileHelper
 
         ContentValues values = new ContentValues();
         values.Put(MediaStore.IMediaColumns.DisplayName, fileName);
-        values.Put(MediaStore.IMediaColumns.MimeType, "text/json");
+        values.Put(MediaStore.IMediaColumns.MimeType, "application/json");
 
         values.Put(MediaStore.IMediaColumns.RelativePath, $"{Android.OS.Environment.DirectoryDownloads}/GES_PAC");
 
diff --git a/GES_PAC/Services/AppDbContext.cs b/GES_PAC/Services/AppDbContext.cs
new file mode 100644
index 0000000..8738295
--- /dev/null
+++ b/GES_PAC/Services/AppDbContext.cs
@@ -0,0 +1,31 @@
+﻿using Microsoft.EntityFrameworkCore;
+using GES_PAC.Model;
+
+public class AppDbContext : DbContext
+{
+    public AppDbContext() { }
+
+    public DbSet<Calibration> Calibration { get; set; }
+    public DbSet<Comportement> Comportement { get; set; }
+    public DbSet<Journee> Journee { get; set; }
+    public DbSet<Lieu> Lieu { get; set; }
+    public DbSet<Mesure> Mesure { get; set; }
+    public DbSet<MesureCalibration> MesureCalibration { get; set; }
+    public DbSet<Personne> Personne { get; set; }
+    public DbSet<PhaseCalibration> PhaseCalibration { get; set; }
+    public DbSet<Serie> Serie { get; set; }
+    public DbSet<SerieAnimal> SerieAnimal { get; set; }
+    public DbSet<TypeCalibration> TypeCalibration { get; set; }
+    public DbSet<TypeComportement> TypeComportement { get; set; }
+
+    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
+    {
+        string dbPath = Path.Combine(FileSystem.AppDataDirectory, "GES_PAC.db3");
+        optionsBuilder.UseSqlite($"Filename={dbPath}");
+    }
+
+    protected override void OnModelCreating(ModelBuilder modelBuilder)
+    {
+
+    }
+}
diff --git a/GES_PAC/ViewModel/CreateBehaviourViewModel.cs b/GES_PAC/ViewModel/CreateBehaviourViewModel.cs
index 7ed3255..a4a42cf 100644
--- a/GES_PAC/ViewModel/CreateBehaviourViewModel.cs
+++ b/GES_PAC/ViewModel/CreateBehaviourViewModel.cs
@@ -93,6 +93,9 @@ namespace GES_PAC.ViewModel
             var currentSet = JourneeViewModel.Instance.GetCurrentDay().GetCurrentSet();
             currentSet.AddBehaviour(newBehaviour, ChamberId, IsAnimalOut);
 
+            App.Db.Comportement.Add(newBehaviour);
+            await App.Db.SaveChangesAsync();
+
             await Shell.Current.Navigation.PopToRootAsync();
             IsBusy = false;
         }
diff --git a/GES_PAC/ViewModel/CreateCalibrationViewModel.cs b/GES_PAC/ViewModel/CreateCalibrationViewModel.cs
index 737f027..9a0165a 100644
--- a/GES_PAC/ViewModel/CreateCalibrationViewModel.cs
+++ b/GES_PAC/ViewModel/CreateCalibrationViewModel.cs
@@ -1,5 +1,6 @@
 using GES_PAC.Model;
 using GES_PAC.View;
+using Microsoft.EntityFrameworkCore;
 using System.Collections.ObjectModel;
 using System.Windows.Input;
 
@@ -11,6 +12,7 @@ namespace GES_PAC.ViewModel
 
         #region Attributs
         public string _phaseName;
+        private ObservableCollection<TypeCalibration> _typeCalibrations;
         private TypeCalibration _selectedType;
         private double? _conc_o2;
         private double? _conc_co2;
@@ -36,19 +38,30 @@ namespace GES_PAC.ViewModel
             get => _phaseName;
             set
             {
+                if (_phaseName == value) return;
                 _phaseName = value;
                 Phase = PhaseCalibration.All.FirstOrDefault(p => p.Name == value) ?? PhaseCalibration.DEBUT;
                 OnPropertyChanged();
+                OnPhaseChanged();
             }
         }
-        public ObservableCollection<TypeCalibration> TypeCalibrations { get; }
+        public ObservableCollection<TypeCalibration> TypeCalibrations
+        {
+            get => _typeCalibrations;
+            set
+            {
+                _typeCalibrations = value;
+                OnPropertyChanged();
+            }
+        }
+
         public TypeCalibration SelectedType
         {
             get => _selectedType;
             set
             {
                 _selectedType = value;
-                IsRefBouteilleVisible = _selectedType != TypeCalibration.Air;
+                IsRefBouteilleVisible = _selectedType != TypeCalibration.AIR;
                 OnPropertyChanged();
                 OnTypeChanged();
                 ValidateForm();
@@ -173,32 +186,51 @@ namespace GES_PAC.ViewModel
         public CreateCalibrationViewModel()
         {
             CreateCalibrationCommand = new Command(async () => await CreateCalibration());
+        }
+        #endregion
 
+        #region Méthodes
+
+        public void OnPhaseChanged()
+        {
             var journeeActuelle = JourneeViewModel.Instance.GetCurrentDay();
-            var typesManquants = journeeActuelle.GetCurrentMissingCalibration();
-            var phaseCalibration = journeeActuelle.GetCurrentPhase();
+            var typesManquants = journeeActuelle.GetMissingCalibrationByPhase(Phase);
 
             TypeCalibrations = new ObservableCollection<TypeCalibration>(typesManquants);
             SelectedType = TypeCalibrations[0];
 
-            if (journeeActuelle.GetCurrentPhase() == PhaseCalibration.FIN)
-            { 
+            if (Phase == PhaseCalibration.FIN)
+            {
                 LastCalibration = journeeActuelle.GetDebutCalibration();
                 HasLastMeasure = true;
+                OnTypeChanged();
             }
         }
-        #endregion
-
-        #region Méthodes
         private async Task CreateCalibration()
         {
             if (IsBusy) return;
             IsBusy = true;
 
+            var journee = JourneeViewModel.Instance.GetCurrentDay();
             var date = DateTime.Now;
             var newCalib = new MesureCalibration(date, ConcO2 ?? 0, ConcCO2 ?? 0, ConcCH4 ?? 0, SelectedType, RefBouteille);
-            var isComplete = JourneeViewModel.Instance.GetCurrentDay().AddCalibration(newCalib, Phase);
 
+            var calibration = journee.Calibrations.FirstOrDefault(c => c.Phase == Phase);
+            if (calibration == null)
+            {
+                calibration = new Calibration(Phase, newCalib);
+                journee.AddCalibration(calibration);
+                App.Db.Calibration.Add(calibration);
+            }
+            else
+            {
+                calibration.AddMesure(newCalib);
+            }
+
+            App.Db.MesureCalibration.Add(newCalib);
+            await App.Db.SaveChangesAsync();
+
+            var isComplete = calibration.IsComplete();
             var targetView = $"{nameof(CreateCalibrationView)}?phase={PhaseName}";
             if (isComplete) {
                 targetView = Phase == PhaseCalibration.DEBUT ? nameof(SetListView) : nameof(MainView);
diff --git a/GES_PAC/ViewModel/CreateDayViewModel.cs b/GES_PAC/ViewModel/CreateDayViewModel.cs
index 414ccdd..49074c1 100644
--- a/GES_PAC/ViewModel/CreateDayViewModel.cs
+++ b/GES_PAC/ViewModel/CreateDayViewModel.cs
@@ -90,6 +90,8 @@ namespace GES_PAC.ViewModel
             IsBusy = true;
             var date = DateTime.Now;
             var newDay = new Journee(date, SelectedPerson, SelectedPlace, Regime);
+            App.Db.Journee.Add(newDay);
+            await App.Db.SaveChangesAsync();
             JourneeViewModel.Instance.Journees.Add(newDay);
             await Shell.Current.GoToAsync($"{nameof(CreateCalibrationView)}?phase={PhaseCalibration.DEBUT.Name}");
             IsBusy = false;
diff --git a/GES_PAC/ViewModel/CreateMeasureViewModel.cs b/GES_PAC/ViewModel/CreateMeasureViewModel.cs
index 1851719..204bde2 100644
--- a/GES_PAC/ViewModel/CreateMeasureViewModel.cs
+++ b/GES_PAC/ViewModel/CreateMeasureViewModel.cs
@@ -198,6 +198,9 @@ namespace GES_PAC.ViewModel
             var currentSet = JourneeViewModel.Instance.GetCurrentDay().GetCurrentSet();
             currentSet.AddMeasure(newMeasure, ChamberId, IsAnimalOut);
 
+            App.Db.Mesure.Add(newMeasure);
+            await App.Db.SaveChangesAsync();
+
             await Shell.Current.Navigation.PopToRootAsync();
             IsBusy = false;
         }
diff --git a/GES_PAC/ViewModel/CreatePersonViewModel.cs b/GES_PAC/ViewModel/CreatePersonViewModel.cs
index 78aac6f..88b00d0 100644
--- a/GES_PAC/ViewModel/CreatePersonViewModel.cs
+++ b/GES_PAC/ViewModel/CreatePersonViewModel.cs
@@ -76,6 +76,9 @@ namespace GES_PAC.ViewModel
         private async Task CreatePerson()
         {
             var newTechnician = new Personne(Nom, Prenom, Email);
+            App.Db.Personne.Add(newTechnician);
+            await App.Db.SaveChangesAsync();
+
             PersonViewModel.Instance.Persons.Add(newTechnician);
             await Shell.Current.GoToAsync("//Main");
         }
diff --git a/GES_PAC/ViewModel/CreatePlaceViewModel.cs b/GES_PAC/ViewModel/CreatePlaceViewModel.cs
index bc5ba26..e8a291c 100644
--- a/GES_PAC/ViewModel/CreatePlaceViewModel.cs
+++ b/GES_PAC/ViewModel/CreatePlaceViewModel.cs
@@ -88,6 +88,9 @@ namespace GES_PAC.ViewModel
         private async Task CreatePlace()
         {
             var newPlace = new Lieu(Nom, Client, Espece);
+            App.Db.Lieu.Add(newPlace);
+            await App.Db.SaveChangesAsync();
+
             PlaceViewModel.Instance.Places.Add(newPlace);
             await Shell.Current.GoToAsync("//Main");
         }
diff --git a/GES_PAC/ViewModel/CreateSetViewModel.cs b/GES_PAC/ViewModel/CreateSetViewModel.cs
index df704ed..a42b059 100644
--- a/GES_PAC/ViewModel/CreateSetViewModel.cs
+++ b/GES_PAC/ViewModel/CreateSetViewModel.cs
@@ -1,5 +1,4 @@
 using GES_PAC.Model;
-using GES_PAC.View;
 using System.Windows.Input;
 
 namespace GES_PAC.ViewModel
@@ -104,6 +103,9 @@ namespace GES_PAC.ViewModel
             var newSet = new Serie(date, DateTimeMAJ, Temperature ?? 0, Humidite ?? 0, Pression ?? 0);
             journeeActuelle.AddSet(newSet);
 
+            App.Db.Serie.Add(newSet);
+            await App.Db.SaveChangesAsync();
+
             await Shell.Current.Navigation.PopToRootAsync();
             IsBusy = false;
         }
diff --git a/GES_PAC/ViewModel/EnterAnimalViewModel.cs b/GES_PAC/ViewModel/EnterAnimalViewModel.cs
index 3748dc3..34e55b5 100644
--- a/GES_PAC/ViewModel/EnterAnimalViewModel.cs
+++ b/GES_PAC/ViewModel/EnterAnimalViewModel.cs
@@ -89,6 +89,9 @@ namespace GES_PAC.ViewModel
             var newAnimalSet = new SerieAnimal(ChamberId, Poids ?? 0, date, NumRFID);
             journeeActuelle.GetCurrentSet().AddSerieAnimal(newAnimalSet);
 
+            App.Db.SerieAnimal.Add(newAnimalSet);
+            await App.Db.SaveChangesAsync();
+
             await Shell.Current.GoToAsync($"{nameof(CreateMeasureView)}?chamberId={ChamberId}");
             IsBusy = false;
         }
diff --git a/GES_PAC/ViewModel/ExportDataViewModel.cs b/GES_PAC/ViewModel/ExportDataViewModel.cs
index d874b0a..0fe544f 100644
--- a/GES_PAC/ViewModel/ExportDataViewModel.cs
+++ b/GES_PAC/ViewModel/ExportDataViewModel.cs
@@ -3,6 +3,7 @@ using GES_PAC.Model;
 using GES_PAC.Helpers;
 using System.Text.Json;
 using System.Collections.ObjectModel;
+using System.Text.Json.Serialization;
 
 
 namespace GES_PAC.ViewModel
@@ -61,7 +62,11 @@ namespace GES_PAC.ViewModel
             IsBusy = true;
             var lastDay = DayList.Last();
             string fileName = lastDay.Date.ToString();
-            string json = JsonSerializer.Serialize(lastDay);
+            string json = JsonSerializer.Serialize(lastDay, new JsonSerializerOptions
+            {
+                ReferenceHandler = ReferenceHandler.Preserve,
+                WriteIndented = true
+            });
             AndroidFileHelper.SaveJsonToDownloads(fileName, json);
             IsBusy = false;
 #endif
diff --git a/GES_PAC/ViewModel/JourneeViewModel.cs b/GES_PAC/ViewModel/JourneeViewModel.cs
index 81ae65b..bf282b4 100644
--- a/GES_PAC/ViewModel/JourneeViewModel.cs
+++ b/GES_PAC/ViewModel/JourneeViewModel.cs
@@ -1,4 +1,5 @@
 using GES_PAC.Model;
+using Microsoft.EntityFrameworkCore;
 using System.Collections.ObjectModel;
 
 namespace GES_PAC.ViewModel
@@ -15,18 +16,50 @@ namespace GES_PAC.ViewModel
         #endregion
 
         #region Constructeurs
-        private JourneeViewModel() { }
+        private JourneeViewModel() {
+            _ = InitAsync();
+        }
         #endregion
 
         #region Méthodes
-        public Journee GetCurrentDay()
+        public async Task InitAsync()
         {
-            DateTime today = DateTime.Today;
-            return Journees.LastOrDefault(j => j.Date.Date == today);
+            var journees = await App.Db.Journee
+                .Include(j => j.Calibrations)
+                    .ThenInclude(c => c.Mesures)
+                        .ThenInclude(m => m.Type)
+                .Include(j => j.Calibrations)
+                    .ThenInclude(c => c.Phase)
+                .Include(j => j.Series)
+                    .ThenInclude(s => s.SeriesAnimales)
+                        .ThenInclude(sa => sa.Mesures)
+                .Include(j => j.Series)
+                    .ThenInclude(s => s.SeriesAnimales)
+                        .ThenInclude(sa => sa.Comportements)
+                .ToListAsync();
+
+
+            foreach (var j in journees)
+            {
+                foreach (var c in j.Calibrations)
+                {
+                    c.Phase = PhaseCalibration.All.FirstOrDefault(p => p.Value == c.Phase?.Value);
+                    foreach (var m in c.Mesures)
+                    {
+                        m.Type = TypeCalibration.All.FirstOrDefault(t => t.Value == m.Type?.Value);
+                    }
+                }
+
+                Journees.Add(j);
+            }
         }
-        public PhaseCalibration GetCurrentPhase()
+        public Journee? GetCurrentDay()
         {
-            return GetCurrentDay().GetCurrentPhase();
+            DateTime today = DateTime.Today;
+            var lastDay = Journees.LastOrDefault(j => j.Date.Date == today);
+            if (lastDay == null || lastDay.IsComplete())
+                return null;
+            return lastDay;
         }
         public Serie? GetCurrentSet()
         {
diff --git a/GES_PAC/ViewModel/MainViewModel.cs b/GES_PAC/ViewModel/MainViewModel.cs
index 2a15a88..2528ff5 100644
--- a/GES_PAC/ViewModel/MainViewModel.cs
+++ b/GES_PAC/ViewModel/MainViewModel.cs
@@ -51,17 +51,6 @@ namespace GES_PAC.ViewModel
         #region Constructeur
         public MainViewModel()
         {
-            //ONLY FOR TESTS
-            var date = DateTime.Now.AddHours(-2);
-            var newDay = new Journee(date, PersonViewModel.Instance.Persons[0], PlaceViewModel.Instance.Places[0], "");
-            newDay.AddSet(new Serie(date, date, 25, 25, 25));
-            newDay.AddCalibration(new MesureCalibration(date, 50, 40, 500, TypeCalibration.Air, ""), PhaseCalibration.DEBUT);
-            newDay.AddCalibration(new MesureCalibration(date, 0, 0, 1000000, TypeCalibration.Methane, "dazdazd"), PhaseCalibration.DEBUT);
-            newDay.AddCalibration(new MesureCalibration(date, 30, 30, 3000, TypeCalibration.Melange, "deqsfsdf"), PhaseCalibration.DEBUT);
-            newDay.GetCurrentSet().AddSerieAnimal(new SerieAnimal(1, 500, date, ""));
-            newDay.GetCurrentSet().AddMeasure(new Mesure(date, 50, 50, 50), 1, false);
-            JourneeViewModel.Instance.Journees.Add(newDay);
-            //ONLY FOR TESTS
 
             GoToCreateDayCommand = new Command(async () => await GoToCreateDayPage());
             ResumeDayCommand = new Command(async () => await ResumeDay());
diff --git a/GES_PAC/ViewModel/PersonViewModel.cs b/GES_PAC/ViewModel/PersonViewModel.cs
index 5d0c220..7d3dabc 100644
--- a/GES_PAC/ViewModel/PersonViewModel.cs
+++ b/GES_PAC/ViewModel/PersonViewModel.cs
@@ -1,4 +1,5 @@
 using GES_PAC.Model;
+using Microsoft.EntityFrameworkCore;
 using System.Collections.ObjectModel;
 
 namespace GES_PAC.ViewModel
@@ -16,7 +17,15 @@ namespace GES_PAC.ViewModel
 
         #region Constructeur
         private PersonViewModel() {
-            Persons.Add(new Personne("Bond", "James", "jamesbond@inrae.fr")); // Ajout d'une personne par défaut pour les tests
+            _ = InitAsync();
+        }
+        #endregion
+        #region Méthodes
+        public async Task InitAsync()
+        {
+            var personnes = await App.Db.Personne.ToListAsync();
+            foreach (var p in personnes)
+                Persons.Add(p);
         }
         #endregion
     }
diff --git a/GES_PAC/ViewModel/PlaceViewModel.cs b/GES_PAC/ViewModel/PlaceViewModel.cs
index e65c51a..021bb06 100644
--- a/GES_PAC/ViewModel/PlaceViewModel.cs
+++ b/GES_PAC/ViewModel/PlaceViewModel.cs
@@ -1,7 +1,7 @@
 using GES_PAC.Model;
+using Microsoft.EntityFrameworkCore;
 using System.Collections.ObjectModel;
 
-
 namespace GES_PAC.ViewModel
 {
     public class PlaceViewModel
@@ -17,7 +17,15 @@ namespace GES_PAC.ViewModel
         
         #region Constructeur
         private PlaceViewModel() {
-            Places.Add(new Lieu("Elevage Langlade", "INRAE", "Mouton")); // Ajout d'un lieu par défaut pour les tests
+            _ = InitAsync();
+        }
+        #endregion
+        #region Méthodes
+        public async Task InitAsync()
+        {
+            var lieux = await App.Db.Lieu.ToListAsync();
+            foreach (var l in lieux)
+                Places.Add(l);
         }
         #endregion
     }
