package com.dano.test1.data

import androidx.room.*

/*
Data-Access-Objekte (DAOs) für die Room-Datenbank.
Sie kapseln alle typsicheren Lese-/Schreiboperationen für die Tabellen clients, questionnaires, questions, answers und completed_questionnaires.

Hinweis:
 - Die konkreten Implementierungen erzeugt Room zur Build-Zeit.
 - DAOs werden über die AppDatabase (Room.databaseBuilder(...)) bezogen.
*/

@Dao
interface ClientDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insertClient(client: Client)

    @Query("SELECT * FROM clients WHERE clientCode = :code LIMIT 1")
    suspend fun getClientByCode(code: String): Client?

    @Delete
    suspend fun deleteClient(client: Client)

    @Query("SELECT COUNT(*) FROM clients")
    suspend fun getClientCount(): Int

    @Query("SELECT * FROM clients")
    suspend fun getAllClients(): List<Client>
}

@Dao
interface QuestionnaireDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insertQuestionnaire(questionnaire: Questionnaire)

    @Query("SELECT * FROM questionnaires WHERE id = :id LIMIT 1")
    suspend fun getById(id: String): Questionnaire?

    @Query("SELECT * FROM questionnaires")
    suspend fun getAll(): List<Questionnaire>
}

@Dao
interface QuestionDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insertQuestions(questions: List<Question>)

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insertQuestion(question: Question)

    @Query("SELECT * FROM questions WHERE questionId = :id LIMIT 1")
    suspend fun getById(id: String): Question?

    @Query("SELECT * FROM questions WHERE questionnaireId = :questionnaireId")
    suspend fun getQuestionsForQuestionnaire(questionnaireId: String): List<Question>
}


@Dao
interface AnswerDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAnswers(answers: List<Answer>)

    @Query("""
        SELECT a.* FROM answers a
        INNER JOIN questions q ON a.questionId = q.questionId
        WHERE a.clientCode = :clientCode AND q.questionnaireId = :questionnaireId
    """)
    suspend fun getAnswersForClientAndQuestionnaire(
        clientCode: String,
        questionnaireId: String
    ): List<Answer>

    @Query("SELECT * FROM answers WHERE clientCode = :clientCode")
    suspend fun getAnswersForClient(clientCode: String): List<Answer>

    @Query("""
        DELETE FROM answers 
        WHERE clientCode = :clientCode 
        AND questionId IN (
            SELECT questionId FROM questions WHERE questionnaireId = :questionnaireId
        )
    """)
    suspend fun deleteAnswersForClientAndQuestionnaire(
        clientCode: String,
        questionnaireId: String
    )
}

@Dao
interface CompletedQuestionnaireDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(entry: CompletedQuestionnaire)

    @Query("SELECT * FROM completed_questionnaires WHERE clientCode = :clientCode AND questionnaireId = :questionnaireId LIMIT 1")
    suspend fun getStatus(clientCode: String, questionnaireId: String): CompletedQuestionnaire?

    @Query("SELECT * FROM completed_questionnaires WHERE clientCode = :clientCode")
    suspend fun getAllForClient(clientCode: String): List<CompletedQuestionnaire>

    @Query("SELECT questionnaireId FROM completed_questionnaires WHERE clientCode = :clientCode")
    suspend fun getCompletedQuestionnairesForClient(clientCode: String): List<String>
}
