version: 2

models:
  - name: V_FS_EMAIL_CAMPAIGN
    description: |
      Email campaigns sent on behalf of artists (global participants) or custom CRM lists.
      Each row represents a single email campaign with its delivery and engagement analytics. 
      Access is restricted by row-level security — only campaigns
      belonging to vendors the current user is authorised to view are returned.
    config:
      persist_docs:
        relation: true
        columns: true
    columns:
      - name: ID
        description: Unique identifier of the email campaign.
        tests:
          - unique
          - not_null
      - name: NAME
        description: Display name of the email campaign.
        tests:
          - not_null
      - name: GLOBAL_PARTICIPANT_ID
        description: >
          Foreign key to the artist (global participant) this campaign was sent on behalf of.
          Null when the campaign targets a custom CRM list rather than a direct artist.
      - name: GLOBAL_PARTICIPANT_NAME
        description: Display name of the artist (global participant) this campaign was sent on behalf of.
      - name: CUSTOM_LIST_ID
        description: >
          Foreign key to the custom CRM list used as the target audience for this campaign.
          Null when the campaign is associated directly with an artist.
      - name: CUSTOM_LIST_NAME
        description: Display name of the custom CRM list used as the target audience.
      - name: VENDOR_ID
        description: Foreign key to the vendor (label or business unit) that owns this campaign.
        tests:
          - not_null
      - name: VENDOR_NAME
        description: Display name of the vendor (label or business unit) that owns this campaign.
      - name: STATUS
        description: Current lifecycle status of the email campaign. One of ('DRAFT', 'SCHEDULED', 'SENT', 'CANCELLED', 'IN_PROGRESS').
        tests:
          - not_null
      - name: SEND_AT
        description: >
          Scheduled or actual send timestamp of the campaign in UTC.
          Null for drafts that have not yet been scheduled.
      - name: AUDIENCE_NAME
        description: >
          Name of the Fansifter audience targeted by this campaign.
          Null when no audience was assigned.
      - name: RECIPIENTS_COUNT
        description: >
          Number of recipients in the targeted audience.
          Only populated when an audience segment is assigned; null otherwise.
      - name: SENDS
        description: Total number of emails sent.
      - name: BOUNCES
        description: Number of unique email bounces.
      - name: BOUNCE_RATE
        description: Ratio of unique bounces to total sends.
      - name: DELIVERED
        description: Number of emails successfully delivered.
      - name: DELIVERED_RATE
        description: Ratio of delivered emails to total sends.
      - name: OPENS
        description: Total number of email open events.
      - name: UNIQUE_OPENS
        description: Number of unique recipients who opened the email at least once.
      - name: OPEN_RATE
        description: Ratio of unique opens to delivered emails.
      - name: CLICKS
        description: Total number of link click events.
      - name: UNIQUE_CLICKS
        description: Number of unique recipients who clicked at least one link.
      - name: CLICK_THROUGH_RATE
        description: Ratio of unique clicks to delivered emails.
      - name: CLICK_TO_OPEN_RATE
        description: Ratio of unique clicks to unique opens.
      - name: UNSUBSCRIBES
        description: Number of recipients who unsubscribed via this campaign.
      - name: SPAM_COMPLAINTS
        description: Number of unique spam complaints filed against this campaign.
      - name: SPAM_COMPLAINTS_RATE
        description: Ratio of spam complaints to total sends.
      - name: CREATED_AT
        description: Timestamp when the campaign record was created, in UTC.
        tests:
          - not_null
      - name: CREATED_BY
        description: Email address of the user who created the campaign.
      - name: LAST_UPDATED_AT
        description: Timestamp of the most recent update to the campaign record, in UTC.
      - name: LAST_UPDATED_BY
        description: Email address of the user who last updated the campaign.

  - name: V_FS_EMAIL_AUTOMATED
    description: |
      Automated (triggered / double opt-in) email templates configured on behalf of artists (global participants)
      or custom CRM lists. Each row represents a single automated email with aggregated
      lifetime delivery and engagement analytics.
      Access is restricted by row-level security — only emails belonging to vendors the
      current user is authorised to view are returned.
    config:
      persist_docs:
        relation: true
        columns: true
    columns:
      - name: ID
        description: Unique identifier of the automated email.
        tests:
          - unique
          - not_null
      - name: NAME
        description: Display name of the automated email template.
        tests:
          - not_null
      - name: TYPE
        description: >
          Type of automated email. TRIGGERED emails are sent in response to fan actions
          (e.g. a new fan joining a newsletter). DOUBLE_OPT_IN emails are sent to confirm a fan's
          subscription opt-in.
        tests:
          - not_null
          - accepted_values:
              arguments:
                values: ['TRIGGERED', 'DOUBLE_OPT_IN']
      - name: GLOBAL_PARTICIPANT_ID
        description: >
          Foreign key to the artist (global participant) this automated email was configured for.
          Null when associated with a custom CRM list rather than a direct artist.
      - name: GLOBAL_PARTICIPANT_NAME
        description: Display name of the artist (global participant) this automated email was configured for.
      - name: CUSTOM_LIST_ID
        description: >
          Foreign key to the custom CRM list this automated email was configured for.
          Null when associated directly with an artist.
      - name: CUSTOM_LIST_NAME
        description: Display name of the custom CRM list this automated email was configured for.
      - name: VENDOR_ID
        description: Foreign key to the vendor (label or business unit) that owns this automated email.
        tests:
          - not_null
      - name: VENDOR_NAME
        description: Display name of the vendor (label or business unit) that owns this automated email.
      - name: IS_AUTOMATED
        description: >
          Boolean flag indicating whether this email has at least one active trigger (e.g. SMF form) configured.
          False means the template exists but has no triggers attached yet.
        tests:
          - not_null
      - name: LAST_SENT_AT
        description: >
          Timestamp of the most recent send of this automated email, in UTC.
          Null if the email has never been sent.
      - name: SENDS
        description: Total number of emails sent across all trigger events.
      - name: BOUNCES
        description: Number of unique email bounces.
      - name: BOUNCE_RATE
        description: Ratio of unique bounces to total sends.
      - name: DELIVERED
        description: Number of emails successfully delivered.
      - name: DELIVERED_RATE
        description: Ratio of delivered emails to total sends.
      - name: OPENS
        description: Total number of open events.
      - name: UNIQUE_OPENS
        description: Number of unique recipients who opened the email at least once.
      - name: OPEN_RATE
        description: Ratio of unique opens to delivered emails.
      - name: CLICKS
        description: Total number of link click events.
      - name: UNIQUE_CLICKS
        description: Number of unique recipients who clicked at least one link.
      - name: CLICK_THROUGH_RATE
        description: Ratio of unique clicks to delivered emails.
      - name: CLICK_TO_OPEN_RATE
        description: Ratio of unique clicks to unique opens.
      - name: UNSUBSCRIBES
        description: Number of recipients who unsubscribed via this automated email.
      - name: SPAM_COMPLAINTS
        description: Number of unique spam complaints filed against this automated email.
      - name: SPAM_COMPLAINTS_RATE
        description: Ratio of spam complaints to total sends.
      - name: CREATED_AT
        description: Timestamp when the automated email record was created, in UTC.
        tests:
          - not_null
      - name: CREATED_BY
        description: Email address of the user who created the automated email.
      - name: LAST_UPDATED_AT
        description: Timestamp of the most recent update to the automated email record, in UTC.
      - name: LAST_UPDATED_BY
        description: Email address of the user who last updated the automated email.
