<?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($genre = '', $order = 'asc', $artist = '', $query = '') {
        $this->db->select('album.name, album.id, year, artist.name as artistName, genre.name as genreName, jpeg');
        $this->db->from('album');
        $this->db->join('artist', 'album.artistid = artist.id');
        $this->db->join('genre', 'genre.id = album.genreid');
        $this->db->join('cover', 'cover.id = album.coverid');

        if(!empty($genre)){
            $this->db->where('genre.name', $genre);
        }

        if(!empty($artist)){
            $this->db->where('artist.name', $artist);
        }

        if($order == 'asc' || $order == 'desc'){
            $this->db->order_by('album.name', $order);
        }

        if(!empty($query)){
            $this->db->like('album.name', $query);
        }

        $query = $this->db->get();
        return $query->result();
    }

    public function researchtype(){
        $this->db->select('name');
        $this->db->from('genre');
        $query = $this->db->get();
        return $query->result();
    }

    public function nameArtist(){
        $this->db->select('name');
        $this->db->from('artist');
        $query = $this->db->get();
        return $query->result();
    }

    public function getArtists($genre = '', $order = 'asc', $query = '') {
        $this->db->select('artist.name as artistName, artist.id as artistId');
        $this->db->from('artist');
        $this->db->join('album', 'album.artistid = artist.id');
        $this->db->join('genre', 'genre.id = album.genreid');
        $this->db->join('cover', 'cover.id = album.coverid');
    
        $this->db->distinct("artist.name");
    
        if (!empty($genre)) {
            $this->db->where('genre.name', $genre);
        }
    
        if ($order == 'asc' || $order == 'desc') {
            $this->db->order_by('artist.name', $order);
        }
    
        if (!empty($query)) {
            $this->db->like('artist.name', $query);
        }
    
        $result = $this->db->get();
        return $result->result();
    }
    

    public function getMusics($genre = '', $order = '', $artist = '', $query = '') {
        $this->db->select('album.name as albumName, album.id as albumId, year, artist.name as artistName, genre.name as genreName, jpeg, song.name as trackName, track.id as trackId');
        $this->db->from('track');
        $this->db->join('album', 'track.albumId = album.id');
        $this->db->join('song', 'song.id = track.songId');
        $this->db->join('artist', 'album.artistid = artist.id');
        $this->db->join('genre', 'genre.id = album.genreid');
        $this->db->join('cover', 'cover.id = album.coverid');
        $this->db->limit(100);

        if (!empty($genre)) {
            $this->db->where('genre.name', $genre);
        }

        if (!empty($artist)) {
            $this->db->where('artist.name', $artist);
        }

        if ($order == 'asc' || $order == 'desc') {
            $this->db->order_by('song.name', $order);
        }

        if (!empty($query)) {
            $this->db->like('song.name', $query);
        }

        $result = $this->db->get();
        return $result->result();
    }

    public function getAlbumDetails($albumId) {
        // Get album info
        $this->db->select('album.name as albumName, album.id, year, artist.name as artistName, genre.name as genreName, jpeg');
        $this->db->from('album');
        $this->db->join('artist', 'album.artistid = artist.id');
        $this->db->join('genre', 'genre.id = album.genreid');
        $this->db->join('cover', 'cover.id = album.coverid');
        $this->db->where('album.id', $albumId);
        $albumQuery = $this->db->get();
        $albumDetails = $albumQuery->row();
    
        // Get album songs
        $this->db->select('song.name as trackName, track.id as trackId');
        $this->db->from('track');
        $this->db->join('song', 'track.songId = song.id');
        $this->db->where('track.albumId', $albumId);
        $songsQuery = $this->db->get();
        $songs = $songsQuery->result();
    
        return array('album' => $albumDetails, 'songs' => $songs);
    }

    public function getArtistDetails($artistId) {
        // Get artist info
        $this->db->select('artist.name as artistName, artist.id');
        $this->db->from('artist');
        $this->db->where('artist.id', $artistId);
        $artistQuery = $this->db->get();
        $artistDetails = $artistQuery->row();
    
        // Get artist's albums with their songs
        $this->db->select('album.id as albumId, album.name as albumName, album.year');
        $this->db->from('album');
        $this->db->where('album.artistid', $artistId);
        $albumsQuery = $this->db->get();
        $albums = $albumsQuery->result();
    
        // Get songs for each album
        foreach ($albums as $album) {
            $this->db->select('track.id as trackId, song.name as songName');
            $this->db->from('track');
            $this->db->join('song', 'track.songid = song.id');
            $this->db->where('track.albumid', $album->albumId);
            $album->songs = $this->db->get()->result();
        }
    
        return array('artist' => $artistDetails, 'albums' => $albums);
    }
    
    
    public function getSongDetails($songId) {
        $this->db->select('song.name as songName, song.id as songId, album.name as albumName, album.id as albumId, album.year, artist.name as artistName, artist.id as artistId, cover.jpeg');
        $this->db->from('track');
        $this->db->join('album', 'track.albumId = album.id');
        $this->db->join('song', 'song.id = track.songId');
        $this->db->join('artist', 'album.artistid = artist.id');
        $this->db->join('cover', 'cover.id = album.coverid');
        $this->db->where('track.id', $songId);

        $result = $this->db->get();
        return $result->row_array();
    }

    // Suppression de la deuxième méthode researchtype

    public function research(){
        $result = $this->db->query();
    }
}