CREATE OR REPLACE VIEW v_finops_tag_keys_to_qualify AS
SELECT * FROM finops_tag_key
WHERE nature IS NULL AND processing_policy <> 'IGNORE' AND first_seen_date IS NOT NULL;

-- Consumers filter discovered_at for their own "recent" window.
-- A historical replay updating first_seen_date cannot duplicate an event.
CREATE OR REPLACE VIEW v_finops_tag_recent_discoveries AS
SELECT e.*, k.origin, k.source_field, k.namespace, k.source_key,
       k.classification, k.nature, k.functional_type, k.processing_policy, v.source_value
FROM finops_tag_event e
JOIN finops_tag_key k ON k.key_hash = e.key_hash
LEFT JOIN finops_tag_value v ON v.value_hash = e.value_hash
WHERE e.is_historical = 0
  AND e.first_seen_date IS NOT NULL;

-- Business alert candidates only, not a notification delivery queue.
-- System/technical execution IDs remain discoverable without value-alert noise.
CREATE OR REPLACE VIEW v_finops_tag_recent_pairs AS
SELECT * FROM v_finops_tag_recent_discoveries
WHERE event_type = 'VALUE_DISCOVERED'
  AND nature = 'BUSINESS' AND processing_policy = 'NORMALIZE';

CREATE OR REPLACE VIEW v_finops_tag_business_candidates AS
SELECT * FROM finops_tag_key
WHERE nature = 'BUSINESS' AND processing_policy = 'NORMALIZE'
  AND canonical_key_id IS NULL AND first_seen_date IS NOT NULL;

CREATE OR REPLACE VIEW v_finops_tag_analysis_keys AS
SELECT * FROM finops_tag_key
WHERE processing_policy = 'ANALYZE' AND first_seen_date IS NOT NULL;

-- Raw presence and useful business coverage are deliberately different.
-- Empty/whitespace-only values do not satisfy business coverage.
CREATE OR REPLACE VIEW v_finops_tag_billing_line_coverage AS
SELECT l.line_hash, l.provider, l.business_date, l.project_id,
       COUNT(o.observation_hash) AS observed_pairs,
       COALESCE(MAX(CASE WHEN k.nature = 'BUSINESS'
                          AND k.processing_policy = 'NORMALIZE'
                          AND CHAR_LENGTH(TRIM(v.source_value)) > 0
                         THEN 1 ELSE 0 END), 0) AS has_business_label
FROM finops_tag_billing_line l
LEFT JOIN finops_tag_observation o ON o.line_hash = l.line_hash
LEFT JOIN finops_tag_key k ON k.key_hash = o.key_hash
LEFT JOIN finops_tag_value v ON v.value_hash = o.value_hash
GROUP BY l.line_hash, l.provider, l.business_date, l.project_id;

CREATE OR REPLACE VIEW v_finops_tag_billing_coverage AS
SELECT provider, business_date, project_id, COUNT(*) AS billing_lines,
       SUM(CASE WHEN observed_pairs = 0 THEN 1 ELSE 0 END) AS lines_without_any_label_or_tag,
       SUM(CASE WHEN has_business_label = 0 THEN 1 ELSE 0 END) AS lines_without_business_label,
       SUM(has_business_label) AS lines_with_business_label
FROM v_finops_tag_billing_line_coverage
GROUP BY provider, business_date, project_id;

-- Mapping a raw value to a value belonging to a different canonical key
-- is an administrative error, never a reason to silently change discovery.
CREATE OR REPLACE VIEW v_finops_tag_mapping_errors AS
SELECT v.value_hash, k.key_hash, k.canonical_key_id,
       c.canonical_key_id AS value_canonical_key_id
FROM finops_tag_value v
JOIN finops_tag_key k ON k.key_hash = v.key_hash
JOIN finops_tag_canonical_value c ON c.id = v.canonical_value_id
WHERE k.canonical_key_id IS NULL OR k.canonical_key_id <> c.canonical_key_id;
