//
//  ConsentDocument.swift
//  SAGE_RK_ML
//
//  Created by Juan David Lopez Regalado on 31/01/25.
//

import Foundation
import ResearchKit

public var ConsentDocument: ORKConsentDocument {

    let consentDocument = ORKConsentDocument()
    consentDocument.title = "Conset for questionaires IPAQ and MMSE" // Title

    // Type of sections
    let sectionTypes: [ORKConsentSectionType] = [
        .overview,
        .dataGathering,
        .privacy,
        .dataUse,
        .timeCommitment,
        .studySurvey,
        .studyTasks,
        .withdrawing
    ]

    let text = [
        "Section 1: Welcome. This study is about...",
        "Section 2: Data Gathering. This study will collect data from your Apple Watch...",
        "Section 3: Privacy. We value your privacy...",
        "Section 4: Data Use. The data collected will be used for...",
        "Section 5: Time Commitment. This study will take you roughly...",
        "Section 6: Study Survey. For this study, you will need to fill out a survey...",
        "Section 7: Study Tasks. You will be requested to do these tasks...",
        "Section 8: Withdrawing. To withdraw from the study..."
    ]

    // Create sections and add them to the consent document
    var sections: [ORKConsentSection] = []
    
    for (index, sectionType) in sectionTypes.enumerated() {
        let section = ORKConsentSection(type: sectionType)
        let localizedText = NSLocalizedString(text[index], comment: "")
        let localizedSummary = localizedText.components(separatedBy: ".")[0] + "."

        section.summary = localizedSummary
        section.content = localizedText
        sections.append(section)
    }
    
    consentDocument.sections = sections

    // add sign to consent
    let signature = ORKConsentSignature(forPersonWithTitle: "Participante", dateFormatString: nil, identifier: "ConsentDocumentParticipantSignature")
    consentDocument.addSignature(signature)

    return consentDocument
}

