<?php
/*
 * cass_database_initialize - Initialize the database
 *
 * This function is used to initialize the database
 */
function cass_database_initialize(){
    global $wpdb;

    $charset_collate = $wpdb->get_charset_collate();
    
    $sql = "CREATE TABLE IF NOT EXISTS cass_type (
          IDType INT NOT NULL AUTO_INCREMENT,
          name VARCHAR(200) NOT NULL,
          description LONGTEXT NULL,
          PRIMARY KEY (IDType))"; 
      $wpdb->get_results($sql);


    $sql = "CREATE TABLE IF NOT EXISTS cass_group (
        IDGroup INT NOT NULL AUTO_INCREMENT,
        name VARCHAR(200) NOT NULL,
        description LONGTEXT NULL,
        PRIMARY KEY (IDGroup));";
      $wpdb->get_results($sql);


      $sql = "CREATE TABLE IF NOT EXISTS cass_season (
        IDSeason INT NOT NULL AUTO_INCREMENT, 
        name VARCHAR(200) NULL, 
        dateSeasonStart DATE NULL,
        dateSeasonEnd DATE NULL, 
        PRIMARY KEY (IDSeason));";
        $wpdb->get_results($sql);


      $sql = "CREATE TABLE IF NOT EXISTS cass_race (
        IDRace INT NOT NULL AUTO_INCREMENT,
        IDSeason INT NOT NULL, 
        dtCreation DATETIME NOT NULL,
        dtEdition DATETIME NOT NULL,
        name VARCHAR(200) NOT NULL,
        state INT NOT NULL,
        IDWPUser_RL INT NOT NULL,
        IDWPUser_Deputy INT NOT NULL,
        placeName VARCHAR(200) NOT NULL,
        placeLocality VARCHAR(200) NULL,
        placeLatitude VARCHAR(45) NULL,
        placeLongitude VARCHAR(45) NULL,
        start DATETIME NOT NULL,
        end DATETIME NOT NULL,
        subscriptionEnable BOOLEAN,
        subscriptionStart DATETIME NOT NULL,
        subscriptionEnd DATETIME NOT NULL,
        meetingPlaceName VARCHAR(100) NULL,
        meetingPlaceLatitude VARCHAR(45) NULL,
        meetingPlaceLongitude VARCHAR(45) NULL,
        participantMin INT NULL,
        participantMax INT NULL,
        descriptionPublic LONGTEXT NULL,
        descriptionMemberSubscribed LONGTEXT NULL,
        formType INT NULL,
        comment LONGTEXT NULL,
        IDType INT NOT NULL,
        IDGroup INT NOT NULL,
        IDLevel INT NOT NULL,
        PRIMARY KEY (IDRace),
        INDEX fk_Race_Type_idx (IDType ASC),
        INDEX fk_Race_Group_idx (IDGroup ASC),
        INDEX fk_Race_Season_idx (IDSeason ASC),
        INDEX fk_Race_Level_idx (IDLevel ASC),
        CONSTRAINT fk_Race_Season
              FOREIGN KEY (IDSeason)
              REFERENCES cass_season (IDSeason)
              ON DELETE NO ACTION
              ON UPDATE NO ACTION,
        CONSTRAINT fk_Race_Type
          FOREIGN KEY (IDType)
          REFERENCES cass_type (IDType)
          ON DELETE NO ACTION
          ON UPDATE NO ACTION,
        CONSTRAINT fk_Race_Level
          FOREIGN KEY (IDLevel)
          REFERENCES cass_level (IDLevel)
          ON DELETE NO ACTION
          ON UPDATE NO ACTION,
        CONSTRAINT fk_Race_Group
          FOREIGN KEY (IDGroup)
          REFERENCES cass_group (IDGroup)
          ON DELETE NO ACTION
          ON UPDATE NO ACTION);";
          
      $wpdb->get_results($sql);      


      $sql = "CREATE TABLE IF NOT EXISTS cass_material (
        IDMaterial INT NOT NULL AUTO_INCREMENT,
        name VARCHAR(200) NOT NULL,
        description LONGTEXT NULL,
        PRIMARY KEY (IDMaterial));";
      $wpdb->get_results($sql);      

      $sql = "CREATE TABLE IF NOT EXISTS cass_level (
        IDLevel INT NOT NULL AUTO_INCREMENT,
        name VARCHAR(200) NOT NULL,
        type VARCHAR(30) NOT NULL,
        description LONGTEXT NULL,
        level INT NOT NULL,
        PRIMARY KEY (IDLevel));";
      $wpdb->get_results($sql);    

      $sql = "CREATE TABLE IF NOT EXISTS cass_race_material (
        IDRace_material INT NOT NULL AUTO_INCREMENT,
        IDRace INT NOT NULL,
        IDMaterial INT NOT NULL,
        PRIMARY KEY (IDRace_material),
        INDEX fk_Race_material_Race_idx (IDRace ASC),
        INDEX fk_Race_material_Material_idx (IDMaterial ASC),
        CONSTRAINT fk_Race_material_Race
          FOREIGN KEY (IDRace)
          REFERENCES cass_race (IDRace)
          ON DELETE CASCADE
          ON UPDATE NO ACTION,
        CONSTRAINT fk_Race_material_Material
          FOREIGN KEY (IDMaterial)
          REFERENCES cass_material (IDMaterial)
          ON DELETE NO ACTION
          ON UPDATE NO ACTION);";
       $wpdb->get_results($sql);        
       
       
       $sql = "CREATE TABLE IF NOT EXISTS cass_race_user (
        IDRace_user INT NOT NULL AUTO_INCREMENT,
        state INT NOT NULL,
        commentInscription LONGTEXT NULL,
        commentUnscribe LONGTEXT NULL,
        IDWPUser INT NOT NULL,
        IDRace INT NOT NULL,
        PRIMARY KEY (IDRace_user),
        INDEX fk_Race_user_Race_idx (IDRace ASC),
        CONSTRAINT fk_Race_user_Race
          FOREIGN KEY (IDRace)
          REFERENCES cass_race (IDRace)
          ON DELETE CASCADE
          ON UPDATE NO ACTION);";
       $wpdb->get_results($sql);    
       

       $sql = "CREATE TABLE IF NOT EXISTS cass_setting (
        IDSetting INT NOT NULL AUTO_INCREMENT,
        clubName VARCHAR(200) NULL,
        dateSeasonStart DATE NULL,
        dateSeasonEnd DATE NULL,
        smtpServer VARCHAR(200) NULL,
        smtpUser VARCHAR(200) NULL,
        smtpPassword VARCHAR(200) NULL,
        smtpSSL boolean NULL,
        smtpPort INT NULL,
        mailContentParticipiantValidationToRLObject LONGTEXT NULL,
        mailContentParticipiantValidationToRL LONGTEXT NULL,
        mailContentParticipiantExcusationToRLObject LONGTEXT NULL,
        mailContentParticipiantExcusationToRL LONGTEXT NULL,
        mailContentParticipiantValidationToMemberObject LONGTEXT NULL,
        mailContentParticipiantValidationToMember LONGTEXT NULL,
        mailContentParticipiantExcusationToMemberObject LONGTEXT NULL,
        mailContentParticipiantExcusationToMember LONGTEXT NULL,
        mailContentRaceStateChangeObject LONGTEXT NULL,
        mailContentRaceStateChange LONGTEXT NULL,
        PRIMARY KEY (IDSetting));";
        $wpdb->get_results($sql);  


       //Create default setting row
       $sql = "SELECT * FROM cass_setting;";
       $wpdb->get_results($sql); 
       if($wpdb->num_rows == 0 ) {
          $sql = "INSERT INTO cass_setting(clubName) VALUES('Club Alpin Suisse de Sion');";
          $wpdb->get_results($sql); 
       } 
}