<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Appointly_merge_fields extends App_merge_fields
{

    public function build()
    {
        return [
            [
                'name'      => 'Booking Subject',
                'key'       => '{appointment_subject}',
                'available' => [
                    'appointly',
                ],
            ],
            [
                'name'      => 'Booking Description',
                'key'       => '{appointment_description}',
                'available' => [
                    'appointly',
                ],
            ],
            [
                'name'      => 'Booking Client Name',
                'key'       => '{appointment_client_name}',
                'available' => ['appointly'],
            ],
            [
                'name'      => 'Booking Client Email',
                'key'       => '{appointment_client_email}',
                'available' => ['appointly'],
            ], [
                'name'      => 'Booking Google Meet Link',
                'key'       => '{appointment_google_meet_link}',
                'available' => [],
                'templates' => [
                    'appointment-cron-reminder-to-contact',
                    'appointment-cron-reminder-to-staff',
                ]
            ],
            [
                'name'      => 'Booking Client Name',
                'key'       => '{appointment_client_phone}',
                'available' => ['appointly'],
            ],
            [
                'name'      => 'Booking Date',
                'key'       => '{appointment_date}',
                'available' => ['appointly'],
            ],
            [
                'name'      => 'Booking Location',
                'key'       => '{appointment_location}',
                'available' => ['appointly'],
            ],
            [
                'name'      => 'Booking Public Url',
                'key'       => '{appointment_public_url}',
                'available' => ['appointly'],
            ],
            [
                'name'      => 'Booking Admin Url',
                'key'       => '{appointment_admin_url}',
                'available' => [],
                'templates' => [
                    'appointment-approved-to-staff',
                    'appointment-cron-reminder-to-staff',
                    'appointment-cancelled-to-staff',
                ]
            ]
        ];
    }

    /**
     * Merge field for appointments
     *
     * @param mixed $appointment_id
     *
     * @return array
     */
    public function format($appointment_id)
    {
        $fields = [];

        $this->ci->db->where('id', $appointment_id);

        $appointment = $this->ci->db->get(db_prefix() . 'appointly_appointments')->row();

        if ( ! $appointment) return $fields;

        $fields['{appointment_subject}'] = $appointment->subject;
        $fields['{appointment_description}'] = $appointment->description;
        $fields['{appointment_client_name}'] = $appointment->name;
        $fields['{appointment_client_email}'] = $appointment->email;
        $fields['{appointment_client_phone}'] = $appointment->phone;
        $fields['{appointment_google_meet_link}'] = 'N/A';
        if ($appointment->google_meet_link) $fields['{appointment_google_meet_link}'] = $appointment->google_meet_link;
        $fields['{appointment_date}'] = _dt($appointment->date . '' . $appointment->start_hour);
        $fields['{appointment_location}'] = $appointment->address;
        $fields['{appointment_admin_url}'] = admin_url('appointly/appointments/view?appointment_id=' . $appointment->id);
        $fields['{appointment_public_url}'] = site_url('appointly/appointments_public/client_hash?hash=' . $appointment->hash);

        return $fields;
    }

}
