using CVChannel
using Test
using DelimitedFiles
"""
This script looks at the communication value of the
generalized Siddhu channel using SDP relaxations.
Specifically, it shows that 3-sym cv is the same
as cvPPT.
WARNING: This takes hours.
WARNING: Saving data sub-routine only works if length(s_range)>3
"""

@testset "3-symCV of Gen Siddhu not helpful" begin
    println("Here we see the 3-sym cv of the generalized Siddhu channel doesn't")
    println("improve cvPPT though we know cvPPT is loose.")
    s_range = [0:0.1:0.5;]
    μ_range = [0:0.1:1;]
    cv_table = zeros(length(s_range),length(μ_range))
    threesym_cv_table = zeros(length(s_range),length(μ_range))
    s_ctr, μ_ctr = 1,1
    for s_id in s_range
        println("---Now scanning for s=",s_id,"---")
        for μ_id in μ_range
            println("μ=",μ_id)
            genSidChan(X) = generalizedSiddhu(X,s_id,μ_id)
            gensid_chan = Choi(genSidChan,3,3)
            cv_ppt, = pptCV(gensid_chan, :dual)
            cv_3sym, = threeSymCVPrimal(gensid_chan)
            cv_table[s_ctr,μ_ctr] = cv_ppt
            threesym_cv_table[s_ctr,μ_ctr] = cv_3sym
            μ_ctr += 1
        end
        s_ctr += 1
        μ_ctr = 1
    end

    println("Here we verify that cvPPT is approximately 3symCV over the whole range.")
    diff = threesym_cv_table - cv_table
    @test all(diff -> diff < 1e-5, diff[:,:])

    info_vec = Vector{Union{Nothing,String}}(nothing, length(s_range)+1)
    label_vec = hcat("s↓ μ:",μ_range')
    info_vec[1] = "INFO:"
    info_vec[2] = "Generated by three-sym-CV-gen-Siddhu-channel.jl"
    info_vec[3] = "s_range = " * string(s_range)
    info_vec[4] = "μ_range = " * string(μ_range)
    data_to_save = hcat(vcat(label_vec,hcat(s_range,threesym_cv_table)),info_vec)

    println("Here is the 3symCV of the channel:")
    show(stdout, "text/plain", data_to_save)
    println("\nPlease name the file you'd like to write these results to: \n")
    file_name = readline()
    file_to_open = string(file_name,".csv")
    writedlm(file_to_open, data_to_save, ',')
end
