DROP VIEW IF EXISTS ticket_messages_export_view;
DROP VIEW IF EXISTS tickets_export_view;

-- create new views
CREATE VIEW ticket_messages_export_view AS
SELECT
    m.id,
    m.ticket_id,

    projects.name    AS project,
    channels.title   AS channel,

    CASE WHEN categories.title IS NOT NULL THEN
                COALESCE(agg_categories.categories, '') ||  
                COALESCE(CASE WHEN agg_categories.categories <> '' THEN ',' ELSE null END, '')  || categories.title
        ELSE
                COALESCE(agg_categories.categories, '')
    END AS categories,

    clients.name     AS client,
    m.client_id,
    clients.crm_client_id AS client_crm_id,
    users.name       AS "user",
    m.user_id,
    users.email      AS user_email,
    cci.custom_crm_id,

    tickets.assignee_id,
    tickets.accepter_id,

    case when m.autoreply_id is not null then 
    'auto'
    ELSE 
    m.type
    END AS type,
    m.text,
    files.id AS link,

    m.received,
    m.read,
    m.deleted,
    tickets.created_at as ticket_created_at,
    m.created_at as sent_at,
    m.received_at,
    m.read_at

FROM ticket_messages m
    LEFT JOIN tickets ON tickets.id = m.ticket_id
    LEFT OUTER JOIN projects ON projects.id = tickets.project_id
    LEFT OUTER JOIN channels ON channels.id = tickets.channel_id
    LEFT OUTER JOIN custom_crm_ids cci ON (cci.user_id = m.user_id AND cci.project_id = tickets.project_id)
    LEFT OUTER JOIN files ON files.id = m.file_id
    LEFT OUTER JOIN clients ON clients.id = m.client_id
    LEFT OUTER JOIN users ON users.id = m.user_id
    LEFT JOIN categories ON categories.id = tickets.category_id
    LEFT OUTER JOIN (
            SELECT
                t.id,
                COALESCE(string_agg(cat.title, ','), '') AS categories
            FROM tickets t
            LEFT JOIN tickets_to_categories tc ON tc.ticket_id = t.id
            LEFT OUTER JOIN categories cat ON cat.id IN (tc.category_id)
            GROUP BY t.id
        ) AS agg_categories ON agg_categories.id = m.ticket_id;


CREATE VIEW tickets_export_view AS
SELECT
    t.created_at,
    TO_CHAR(t.created_at, 'YYYY-MM-DD HH24:MI:SS') as created_at_text,

    t.id,
    t.initiator_client, 
    t.project_id,
    t.channel_id,
    t.chat_id,
    t.category_id,
    t.client_id,
    
    t.rating_id,
    t.state,
    t.fcr,

    projects.name    AS project,
    clients.name     AS client,
    clients.crm_client_id AS client_crm_id,
    client_details.inn,

    CASE WHEN clients.marker_type = 'red' THEN markers.red_marker_name
         WHEN clients.marker_type = 'green' THEN markers.green_marker_name END
                     AS marker_type,
    channels.title   AS channel,
    CASE WHEN categories.title IS NOT NULL THEN
                COALESCE(agg_categories.categories, '') ||
                COALESCE(CASE WHEN agg_categories.categories <> '' THEN ',' ELSE null END, '')  || categories.title
        ELSE
                COALESCE(agg_categories.categories, '')
    END AS categories,
    groups.id        AS group_id,
    groups.title     AS "group",

    t.assignee_id,
    assignees.name   AS assignee,
    assignees.email  AS assignee_email,
    cci_assignee.custom_crm_id AS assignee_custom_crm_id,

    t.accepter_id,
    accepters.name   AS accepter,
    accepters.email  AS accepter_email,
    cci_accepter.custom_crm_id AS accepter_custom_crm_id,

    ratings.value    AS rating,
    ratings.state    AS rating_state,

    t.blocked,
    TO_CHAR(t.blocked_at, 'YYYY-MM-DD HH24:MI:SS') as blocked_at,
    TO_CHAR(t.blocked_until_time, 'YYYY-MM-DD HH24:MI:SS') as blocked_until_time,
    t.block_reason, 
    t.user_who_blocked_id,
    blocked.name AS user_who_blocked,


    (case
         when t.violation_id is not null then true
         else false
        end) as violation,

    CAST((CASE
              WHEN t.accept_time IS NULL OR t.accept_time = 0 THEN 0
              ELSE t.accept_time
        END) AS INTEGER) as accept_time,

    t.assign_time,
    t.assigned_at,
    t.response_time,
    t.total_response_time,
    t.total_time,

    t.total_messages,
    t.total_members,
    t.total_assignments,
    (CASE 
        WHEN t.total_transferred IS NOT NULL THEN  t.total_transferred
        ELSE 0
    END) as total_transferred,

    t.open,
    t.closed_at,
    TO_CHAR(t.closed_at, 'YYYY-MM-DD HH24:MI:SS') as closed_at_text,
    
    t.ip,
    t.ip_city,
    t.ip_country,

    t.ua,
    t.ua_browser,
    t.ua_browser_version,
    t.ua_mobile,
    t.ua_os,
    t.ua_os_version,
    t.ua_platform,
    t.system_ticket,
    t.deleted

FROM tickets t

JOIN projects ON projects.id = t.project_id
JOIN clients ON clients.id = t.client_id
LEFT OUTER JOIN client_details ON client_details.client_id = clients.id
LEFT OUTER JOIN markers ON  t.project_id = markers.project_id
LEFT OUTER JOIN channels ON channels.id = t.channel_id
LEFT OUTER JOIN groups ON groups.id = t.group_id
LEFT OUTER JOIN ratings ON ratings.id = t.rating_id
LEFT OUTER JOIN users assignees ON assignees.id = t.assignee_id
LEFT OUTER JOIN users accepters ON accepters.id = t.accepter_id
LEFT OUTER JOIN users blocked ON blocked.id = t.user_who_blocked_id

LEFT OUTER JOIN custom_crm_ids cci_assignee ON (cci_assignee.user_id = assignees.id AND cci_assignee.project_id = t.project_id)
LEFT OUTER JOIN custom_crm_ids cci_accepter ON (cci_accepter.user_id = accepters.id AND cci_accepter.project_id = t.project_id)
LEFT JOIN categories ON categories.id = t.category_id
LEFT OUTER JOIN (
           SELECT
                t.id,
                COALESCE(string_agg(cat.title, ','), '') AS categories
            FROM tickets t
            LEFT OUTER JOIN tickets_to_categories tc ON tc.ticket_id = t.id
            LEFT OUTER JOIN categories cat ON cat.id IN (tc.category_id)
            GROUP BY t.id
        ) AS agg_categories ON agg_categories.id = t.id


