//
//  AccountView.swift
//  PadelClub
//
//  Created by Laurent Morvillier on 21/02/2024.
//

import SwiftUI
import LeStorage

struct AccountView: View {

    @Environment(\.dismiss) private var dismiss

    var user: CustomUser
    var handler: () -> ()
    
    var body: some View {
        Form {
            #if DEBUG
            if let purchase = Guard.main.currentBestPurchase, let item = StoreItem(rawValue: purchase.productId) {
                PurchaseView(purchaseRow: PurchaseRow(id: purchase.id, name: purchase.productId, item: item))
            }
            #endif
            Section {
                NavigationLink("Changer de mot de passe") {
                    ChangePasswordView()
                }
            }
            Section {
                RowButtonView("Déconnexion", role: .destructive) {
                    DataStore.shared.disconnect()
                    dismiss()
//                    handler()
                }
            }
            Section {
                RowButtonView("Supprimer mon compte", role: .destructive, confirmationMessage: "Voulez-vous vraiment supprimer définitivement votre compte et ses données associées ?") {
                    DataStore.shared.deleteAccount()
//                    handler()
                }
            }
        }.navigationTitle(user.username)
    }
}

//#Preview {
//    AccountView(user: User(username: "coco", email: ",mail", firstName: "first", lastName: "last", phone: "44444", country: "US")) {
//        
//    }
//}
