<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');                                             

class Model_music extends CI_Model {
	public function __construct(){
		$this->load->database();
	}

	public function getAlbums(){
		$query = $this->db->query(
			"SELECT album.name,album.id as albumId,year,artist.name as artistName, genre.name as genreName,jpeg 
			FROM album 
			JOIN artist ON album.artistid = artist.id
			JOIN genre ON genre.id = album.genreid
			JOIN cover ON cover.id = album.coverid
			ORDER BY year
			"
		);
	return $query->result();
	}

	public function getArtistes(){
		$query = $this->db->query(
			"SELECT artist.name, artist.id
			FROM artist 
			ORDER BY artist.name
			"
		);
	return $query->result();
	}

	public function getChansons(){
		$query = $this->db->query(
			"SELECT song.name,song.id,album.year,album.name as albumName, artist.name as artistName, genre.name as genreName
			FROM song 
			JOIN track ON track.songId = song.id
			JOIN album ON album.id = track.albumId
			JOIN artist ON album.artistid = artist.id
			JOIN genre ON genre.id = album.genreid
			ORDER BY album.year
			"
		);
	return $query->result();
	}

	public function getSearchAlbums($nom){
		$query = $this->db->query(
			"SELECT album.name,album.id as albumId,year,artist.name as artistName, genre.name as genreName,jpeg 
			FROM album 
			JOIN artist ON album.artistid = artist.id
			JOIN genre ON genre.id = album.genreid
			JOIN cover ON cover.id = album.coverid
			WHERE album.name LIKE '$nom'
			"
		);
		//$query->result();
		if ($query->num_rows() > 0){
			return $query->result();
		}
		return $query = false;
	}

	public function getSearchArtistes($nom){
		$query = $this->db->query(
			"SELECT artist.name, artist.id
			FROM artist 
			WHERE artist.name LIKE '$nom'
			"
		);
		$query->result();
		if ($query->num_rows() > 0){
			return $query->result();
		}
		return $query = false;
	}

	public function getSearchChansons($nom){
		$query = $this->db->query(
			"SELECT song.name,song.id,album.year,album.name as albumName, artist.name as artistName, genre.name as genreName
			FROM song 
			JOIN track ON track.songId = song.id
			JOIN album ON album.id = track.albumId
			JOIN artist ON album.artistid = artist.id
			JOIN genre ON genre.id = album.genreid
			WHERE song.name LIKE '$nom'
			
			"
		);
	$query->result();
	if ($query->num_rows() > 0){
		return $query->result();
	}
	return $query = false;
	}

	public function getAlbumChoose($id) {
		$this->db->where('id', $id);
		$query = $this->db->get('album');
		$album = $query->row();
	
		// Recupere la couverture
		$this->db->where('id', $album->coverId); 
		$coverQuery = $this->db->get('cover');
		$cover = $coverQuery->row();
		$album->coverImage = $cover->jpeg;
	// Recupere le l'id de la musique a partie de l'album
		$this->db->select('songId'); 
		$this->db->where('albumId', $id);
		$songsQuery = $this->db->get('track');
		$songIds = $songsQuery->result();
	
		// Récupérer les noms des chansons à partir des songIds
		$album->songs = [];
		foreach ($songIds as $song) {
			$this->db->select('name');
			$this->db->where('id', $song->songId);
			$songQuery = $this->db->get('song');
			$songData = $songQuery->row();
			$album->songs[] = $songData->name;
		}
	
		$album->tracks = [];
		foreach ($songIds as $song) {
			$this->db->select('song.name as songName, track.duration');
			$this->db->from('track');
			$this->db->join('song', 'track.songId = song.id');
			$this->db->where('track.songId', $song->songId);
			$trackQuery = $this->db->get();
			$trackData = $trackQuery->row();
			$album->tracks[] = $trackData;
		}
	
		$this->db->where('id', $album->artistId);
			$artistQuery = $this->db->get('artist');
			$artist = $artistQuery->row();
			$album->artist = $artist;
	
			$this->db->where('id', $album->artistId);
			$artistQuery = $this->db->get('artist');
			$artist = $artistQuery->row();
			$album->artist = $artist;
			
	
	
		return $album;
	}

	public function getAlbumsByArtistId($artistId) {
		$query = $this->db->query(
			"SELECT album.name,album.id as albumId,year,artist.name as artistName, genre.name as genreName,jpeg 
			FROM album 
			JOIN artist ON album.artistid = artist.id
			JOIN genre ON genre.id = album.genreid
			JOIN cover ON cover.id = album.coverid
			WHERE album.artistid=$artistId
			ORDER BY year
			"
		);
	return $query->result();
	}
}
