<?php

require_once BASE_PATH
    . '/config/database.php';


class Contact
{
    private PDO $db;


    public function __construct()
    {
        $database =
            new Database();

        $this->db =
            $database->connect();
    }


    public function create(
        array $data
    ): int {

        $stmt =
            $this->db->prepare("
                INSERT INTO contacts (
                    full_name,
                    phone,
                    email,
                    subject,
                    message,
                    status
                )
                VALUES (
                    :full_name,
                    :phone,
                    :email,
                    :subject,
                    :message,
                    'new'
                )
            ");


        $stmt->execute([
            'full_name' =>
                $data['full_name'],

            'phone' =>
                $data['phone'] ?: null,

            'email' =>
                $data['email'] ?: null,

            'subject' =>
                $data['subject'] ?: null,

            'message' =>
                $data['message']
        ]);


        return (int)
            $this->db->lastInsertId();
    }


    public function countForOwner(
        string $search = '',
        string $status = ''
    ): int {

        $params = [];

        $where = '1 = 1';


        if ($search !== '') {

            $where .= "
                AND (
                    full_name LIKE :search_name
                    OR phone LIKE :search_phone
                    OR email LIKE :search_email
                    OR subject LIKE :search_subject
                )
            ";

            $searchValue = '%' . $search . '%';

            $params['search_name'] = $searchValue;
            $params['search_phone'] = $searchValue;
            $params['search_email'] = $searchValue;
            $params['search_subject'] = $searchValue;
        }


        if (
            in_array(
                $status,
                [
                    'new',
                    'reading',
                    'replied',
                    'closed'
                ],
                true
            )
        ) {

            $where .= "
                AND status = :status
            ";

            $params['status'] =
                $status;
        }


        $stmt =
            $this->db->prepare("
                SELECT COUNT(*)

                FROM contacts

                WHERE {$where}
            ");


        $stmt->execute($params);


        return (int)
            $stmt->fetchColumn();
    }


    public function getForOwner(
        string $search = '',
        string $status = '',
        int $limit = 15,
        int $offset = 0
    ): array {

        $params = [];

        $where = '1 = 1';


        if ($search !== '') {

            $where .= "
                AND (
                    full_name LIKE :search_name
                    OR phone LIKE :search_phone
                    OR email LIKE :search_email
                    OR subject LIKE :search_subject
                )
            ";

            $searchValue = '%' . $search . '%';

            $params['search_name'] = $searchValue;
            $params['search_phone'] = $searchValue;
            $params['search_email'] = $searchValue;
            $params['search_subject'] = $searchValue;
        }


        if (
            in_array(
                $status,
                [
                    'new',
                    'reading',
                    'replied',
                    'closed'
                ],
                true
            )
        ) {

            $where .= "
                AND status = :status
            ";

            $params['status'] =
                $status;
        }


        $limit = max(
            1,
            $limit
        );


        $offset = max(
            0,
            $offset
        );


        $stmt =
            $this->db->prepare("
                SELECT *

                FROM contacts

                WHERE {$where}

                ORDER BY created_at DESC

                LIMIT {$limit}
                OFFSET {$offset}
            ");


        $stmt->execute($params);


        return $stmt->fetchAll();
    }


    public function findById(
        int $id
    ): ?array {

        $stmt =
            $this->db->prepare("
                SELECT *

                FROM contacts

                WHERE id = :id

                LIMIT 1
            ");


        $stmt->execute([
            'id' => $id
        ]);


        $contact =
            $stmt->fetch();


        return $contact ?: null;
    }


    public function updateStatus(
        int $id,
        string $status
    ): void {

        $stmt =
            $this->db->prepare("
                UPDATE contacts

                SET status = :status

                WHERE id = :id
            ");


        $stmt->execute([
            'status' =>
                $status,

            'id' =>
                $id
        ]);
    }
}
