package io.delphiplatform.api.v3.decibelrdb.entity;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import io.delphiplatform.api.util.CollectionUtils;
import io.delphiplatform.api.v3.rdb.entity.ads.AdsAdSetEntity;
import lombok.Getter;
import lombok.Setter;

import static io.delphiplatform.api.v3.constant.DecibelTableNames.CAMPAIGNS;
import static io.delphiplatform.api.v3.constant.DecibelTableNames.DECIBEL_SCHEMA;
import static io.delphiplatform.api.v3.constant.DecibelTableNames.PROJECT_CAMPAIGNS;

@Getter
@Setter
@Entity
@Table(name = CAMPAIGNS, schema = DECIBEL_SCHEMA)
public class DecibelCampaignEntity {

    @Id
    private String id;
    private String name;
    private BigDecimal spend;
    private BigDecimal budget;
    private LocalDate startDate;
    private LocalDate endDate;
    private Boolean isPending;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "provider_id")
    private DecibelCampaignProviderEntity provider;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "objective_id")
    private DecibelCampaignObjectiveEntity objective;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "decibel_label_id")
    private DecibelLabelEntity label;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id")
    private DecibelCampaignCategoryEntity category;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sub_category_id")
    private DecibelCampaignSubCategoryEntity subCategory;

    @OneToMany(mappedBy = "campaignPlatformId.campaign")
    private Set<DecibelCampaignPlatformEntity> platforms;

    @ManyToMany
    @JoinTable(
        name = PROJECT_CAMPAIGNS,
        schema = DECIBEL_SCHEMA,
        joinColumns = {@JoinColumn(name = "campaign_id")},
        inverseJoinColumns = {@JoinColumn(name = "project_id")}
    )
    private Set<DecibelProjectEntity> projects;

    @OneToMany(mappedBy = "decibelCampaign")
    private Set<AdsAdSetEntity> adsAdSets;

    public boolean isAssigned() {
        return CollectionUtils.isNotEmpty(projects);
    }

}
