USE [FirstSolutionDB]
GO

-- ============================================================
-- SP: cedlab_Chart_UL2
-- Metadata dei sottostanti per il grafico V2.
-- NON modifica cedlab_Chart_UL1 (esistente, usata altrove).
--
-- Restituisce (ordinato IsWorstOf DESC, worst-of nella prima riga):
--   IDCertificates, IDUnderlyings, StartDate, Strike,
--   BarrieraCouponPerc, BarrieraCoupon,
--   BarrieraCapitalePerc, BarrieraCapitale,
--   Sottostante, IsWorstOf,
--   PriceWorst, PriceWorstPerc,
--   NumPrezziCFT, NomeCFT,
--   TriggerAutocallPerc, AutocallValue
--
-- Colonne con <<< verify: adatta al tuo schema reale.
-- ============================================================

CREATE OR ALTER PROCEDURE [dbo].[cedlab_Chart_UL2]
    @isin NVARCHAR(12)
AS
BEGIN
    SET NOCOUNT ON;

    -- Recupera IDCertificates
    DECLARE @IDCertificates INT;

    SELECT TOP 1
        @IDCertificates = c.IDCertificates          -- <<< verify: colonna ID nel tuo Certificates
    FROM [dbo].[Certificates] c                      -- <<< verify: nome tabella certificati
    WHERE c.ISIN = @isin;

    IF @IDCertificates IS NULL RETURN;

    -- Conta prezzi EOD disponibili del certificato (usato per il sub-titolo "< 30 gg")
    DECLARE @NumPrezziCFT INT;
    SELECT @NumPrezziCFT = COUNT(*)
    FROM dbo.Prices
    WHERE CertificatesID = @IDCertificates
      AND PX_LAST_EOD IS NOT NULL
      AND PX_LAST_EOD <> 0;

    SELECT
        u.IDCertificates,
        u.IDUnderlyings,
        u.StartDate,                                 -- <<< verify: colonna StartDate in Underlyings
        u.Strike,                                    -- <<< verify: colonna Strike in Underlyings

        -- Barriere (% e assolute)
        u.BarrieraCouponPerc,                        -- <<< verify: colonna % barriera coupon
        u.BarrieraCoupon,                            -- <<< verify: colonna assoluta barriera coupon
        u.BarrieraCapitalePerc,                      -- <<< verify: colonna % barriera capitale
        u.BarrieraCapitale,                          -- <<< verify: colonna assoluta barriera capitale

        u.Name             AS Sottostante,           -- <<< verify: colonna nome sottostante

        u.IsWorstOf,                                 -- <<< verify: colonna 1/0 worst-of in Underlyings

        -- PriceWorst: ultimo Px_close per il worst-of (0 per gli altri)
        CASE
            WHEN u.IsWorstOf = 1 THEN ISNULL(lp.Px_close, 0)
            ELSE 0
        END AS PriceWorst,

        -- PriceWorstPerc: PriceWorst / Strike * 100
        CASE
            WHEN u.IsWorstOf = 1 AND ISNULL(u.Strike, 0) > 0
            THEN ISNULL(lp.Px_close, 0) / u.Strike * 100
            ELSE 0
        END AS PriceWorstPerc,

        @NumPrezziCFT      AS NumPrezziCFT,

        -- NomeCFT: Descrizione + ' - ' + ISIN  (adatta nomi colonne)
        c.Descrizione + ' - ' + c.ISIN AS NomeCFT,  -- <<< verify: colonne Descrizione e ISIN

        ISNULL(u.TriggerAutocallPerc, 0) AS TriggerAutocallPerc,  -- <<< verify
        ISNULL(u.AutocallValue,       0) AS AutocallValue          -- <<< verify

    FROM dbo.Underlyings u
    JOIN [dbo].[Certificates] c                      -- <<< verify
        ON c.IDCertificates = u.IDCertificates
    -- Ultimo prezzo disponibile per ogni sottostante (per PriceWorst)
    LEFT JOIN (
        SELECT
            UnderlyingsID,
            Px_close,
            ROW_NUMBER() OVER (
                PARTITION BY UnderlyingsID
                ORDER BY Px_date DESC
            ) AS rn
        FROM dbo.Prices
        WHERE Px_close IS NOT NULL
    ) lp ON lp.UnderlyingsID = u.IDUnderlyings AND lp.rn = 1

    WHERE u.IDCertificates = @IDCertificates
    ORDER BY u.IsWorstOf DESC;  -- worst-of nella prima riga

END
GO
