-- ══════════════════════════════════════════════════════════════════
-- FIX: recrear vistas con IndActivo
-- ══════════════════════════════════════════════════════════════════

USE Banda;
GO

-- ── 1. Eliminar vistas ─────────────────────────────────────────────

DROP VIEW IF EXISTS Music.VWCancionArtista;
DROP VIEW IF EXISTS Music.VWAlbumArtista;
DROP VIEW IF EXISTS Music.VWAlbumCancion;
DROP VIEW IF EXISTS Music.VWArtistaRol;
DROP VIEW IF EXISTS Music.VWRol;
DROP VIEW IF EXISTS Music.VWClip;
DROP VIEW IF EXISTS Music.VWFoto;
DROP VIEW IF EXISTS Music.VWAlbumFoto;
DROP VIEW IF EXISTS Music.VWVideo;
DROP VIEW IF EXISTS Music.VWArtista;
DROP VIEW IF EXISTS Music.VWAlbum;
DROP VIEW IF EXISTS Music.VWCancion;
DROP VIEW IF EXISTS Music.VWCancionesDisponibles;
DROP VIEW IF EXISTS Music.VWCreditosCancion;
DROP VIEW IF EXISTS Music.VWAlbumTracklist;
GO

-- ── 2. Recrear vistas con IndActivo ───────────────────────────────

CREATE VIEW Music.VWAlbumTracklist AS
SELECT
    a.IdAlbum,
    a.Titulo                AS Album,
    ta.Descripcion          AS TipoAlbum,
    a.FechaLanzamiento,
    a.PortadaRuta,
    ac.NumeroPista,
    c.IdCancion,
    c.Titulo                AS Cancion,
    c.DuracionSegundos,
    c.AudioRuta,
    tec.Descripcion         AS EstadoCancion
FROM Music.Album a
JOIN Music.TipoAlbum ta             ON ta.IdTipoAlbum           = a.IdTipoAlbum
JOIN Music.AlbumCancion ac          ON ac.IdAlbum               = a.IdAlbum
JOIN Music.Cancion c                ON c.IdCancion              = ac.IdCancion AND c.IndActivo = 1
JOIN Music.TipoEstadoCancion tec    ON tec.IdTipoEstadoCancion  = c.IdTipoEstadoCancion
WHERE a.IndActivo = 1;
GO

CREATE VIEW Music.VWCreditosCancion AS
SELECT
    c.IdCancion,
    c.Titulo                AS Cancion,
    ar.IdArtista,
    ar.Nombre               AS Artista,
    tc.Descripcion          AS TipoCredito
FROM Music.Cancion c
JOIN Music.CancionArtista ca    ON ca.IdCancion     = c.IdCancion
JOIN Music.Artista ar           ON ar.IdArtista     = ca.IdArtista
JOIN Music.TipoCredito tc       ON tc.IdTipoCredito = ca.IdTipoCredito
WHERE c.IndActivo = 1;
GO

CREATE VIEW Music.VWCancionesDisponibles AS
SELECT
    c.IdCancion,
    c.Titulo,
    c.DuracionSegundos,
    c.AudioRuta,
    a.IdAlbum,
    a.Titulo                AS Album,
    a.PortadaRuta,
    ac.NumeroPista
FROM Music.Cancion c
JOIN Music.TipoEstadoCancion tec    ON tec.IdTipoEstadoCancion  = c.IdTipoEstadoCancion
JOIN Music.AlbumCancion ac          ON ac.IdCancion             = c.IdCancion
JOIN Music.Album a                  ON a.IdAlbum                = ac.IdAlbum AND a.IndActivo = 1
WHERE c.IndActivo        = 1
  AND tec.Descripcion    = N'Disponible'
  AND c.AudioRuta        IS NOT NULL;
GO

CREATE VIEW Music.VWCancion AS
SELECT
    c.IdCancion,
    c.Titulo,
    c.IdTipoEstadoCancion,
    tec.Descripcion         AS EstadoCancion,
    c.DuracionSegundos,
    c.AudioRuta,
    c.Letra,
    c.IndActivo
FROM Music.Cancion c
JOIN Music.TipoEstadoCancion tec    ON tec.IdTipoEstadoCancion = c.IdTipoEstadoCancion;
GO

CREATE VIEW Music.VWAlbum AS
SELECT
    a.IdAlbum,
    a.Titulo,
    a.IdTipoAlbum,
    ta.Descripcion          AS TipoAlbum,
    a.FechaLanzamiento,
    a.PortadaRuta,
    a.Descripcion,
    a.IndUltimosLanzamientos,
    a.IndActivo
FROM Music.Album a
JOIN Music.TipoAlbum ta     ON ta.IdTipoAlbum = a.IdTipoAlbum;
GO

CREATE VIEW Music.VWArtista AS
SELECT
    a.IdArtista,
    a.Nombre,
    a.Descripcion,
    a.FotoRuta,
    a.IndMiembro,
    a.Orden,
    a.IndActivo
FROM Music.Artista a;
GO

CREATE VIEW Music.VWVideo AS
SELECT
    v.IdVideo,
    v.Titulo,
    v.YoutubeId,
    v.IdCancion,
    c.Titulo                AS Cancion,
    v.FechaPublicacion,
    v.IndActivo
FROM Music.Video v
LEFT JOIN Music.Cancion c   ON c.IdCancion = v.IdCancion;
GO

CREATE VIEW Music.VWAlbumFoto AS
SELECT
    af.IdAlbumFoto,
    af.Titulo,
    af.Descripcion,
    af.FechaEvento,
    af.IndActivo
FROM Music.AlbumFoto af;
GO

CREATE VIEW Music.VWFoto AS
SELECT
    f.IdFoto,
    f.Titulo,
    f.Ruta,
    f.Descripcion,
    f.IdTipoFoto,
    tf.Descripcion          AS TipoFoto,
    f.IdAlbumFoto,
    af.Titulo               AS AlbumFoto,
    f.Orden,
    f.IndActivo
FROM Music.Foto f
LEFT JOIN Music.TipoFoto tf     ON tf.IdTipoFoto  = f.IdTipoFoto
LEFT JOIN Music.AlbumFoto af    ON af.IdAlbumFoto = f.IdAlbumFoto;
GO

CREATE VIEW Music.VWClip AS
SELECT
    cl.IdClip,
    cl.IdCancion,
    c.Titulo                AS Cancion,
    cl.Ruta,
    cl.DuracionSegundos,
    cl.IndActivo
FROM Music.Clip cl
JOIN Music.Cancion c        ON c.IdCancion = cl.IdCancion;
GO

CREATE VIEW Music.VWRol AS
SELECT
    r.IdRol,
    r.Descripcion,
    r.IndActivo
FROM Music.Rol r;
GO

CREATE VIEW Music.VWArtistaRol AS
SELECT
    ar.IdArtistaRol,
    ar.IdArtista,
    a.Nombre                AS Artista,
    ar.IdRol,
    r.Descripcion           AS Rol
FROM Music.ArtistaRol ar
JOIN Music.Artista a        ON a.IdArtista = ar.IdArtista
JOIN Music.Rol r            ON r.IdRol     = ar.IdRol;
GO

CREATE VIEW Music.VWAlbumCancion AS
SELECT
    ac.IdAlbumCancion,
    ac.IdAlbum,
    a.Titulo                AS Album,
    a.FechaLanzamiento,
    ac.NumeroPista,
    ac.IdCancion,
    c.Titulo                AS Cancion
FROM Music.AlbumCancion ac
JOIN Music.Album a          ON a.IdAlbum   = ac.IdAlbum
JOIN Music.Cancion c        ON c.IdCancion = ac.IdCancion;
GO

CREATE VIEW Music.VWAlbumArtista AS
SELECT
    aa.IdAlbumArtista,
    aa.IdAlbum,
    a.Titulo                AS Album,
    aa.IdArtista,
    ar.Nombre               AS Artista,
    aa.IdTipoCredito,
    tc.Descripcion          AS TipoCredito
FROM Music.AlbumArtista aa
JOIN Music.Album a          ON a.IdAlbum        = aa.IdAlbum
JOIN Music.Artista ar       ON ar.IdArtista     = aa.IdArtista
JOIN Music.TipoCredito tc   ON tc.IdTipoCredito = aa.IdTipoCredito;
GO

CREATE VIEW Music.VWCancionArtista AS
SELECT
    ca.IdCancionArtista,
    ca.IdCancion,
    c.Titulo                AS Cancion,
    ca.IdArtista,
    ar.Nombre               AS Artista,
    ca.IdTipoCredito,
    tc.Descripcion          AS TipoCredito
FROM Music.CancionArtista ca
JOIN Music.Cancion c        ON c.IdCancion      = ca.IdCancion
JOIN Music.Artista ar       ON ar.IdArtista     = ca.IdArtista
JOIN Music.TipoCredito tc   ON tc.IdTipoCredito = ca.IdTipoCredito;
GO
