<?php
class ContactPerson {
    private $conn;
    private $table_name = "contact_persons";

    public $id;
    public $client_id;
    public $first_name;
    public $last_name;
    public $email;
    public $phone;
    public $position;
    public $department;
    public $is_primary;
    public $notes;
    public $created_at;
    public $updated_at;

    public function __construct($db) {
        $this->conn = $db;
    }

    public function create() {
        $query = "INSERT INTO " . $this->table_name . " SET client_id=:client_id, first_name=:first_name, last_name=:last_name, email=:email, phone=:phone, position=:position, department=:department, is_primary=:is_primary, notes=:notes, created_at=:created_at, updated_at=:updated_at";

        $stmt = $this->conn->prepare($query);

        $this->client_id = htmlspecialchars(strip_tags($this->client_id));
        $this->first_name = htmlspecialchars(strip_tags($this->first_name));
        $this->last_name = htmlspecialchars(strip_tags($this->last_name));
        $this->email = htmlspecialchars(strip_tags($this->email));
        $this->phone = htmlspecialchars(strip_tags($this->phone));
        $this->position = htmlspecialchars(strip_tags($this->position));
        $this->department = htmlspecialchars(strip_tags($this->department));
        $this->notes = htmlspecialchars(strip_tags($this->notes));
        $this->is_primary = $this->is_primary ? 1 : 0;
        $this->created_at = date('Y-m-d H:i:s');
        $this->updated_at = date('Y-m-d H:i:s');

        $stmt->bindParam(":client_id", $this->client_id);
        $stmt->bindParam(":first_name", $this->first_name);
        $stmt->bindParam(":last_name", $this->last_name);
        $stmt->bindParam(":email", $this->email);
        $stmt->bindParam(":phone", $this->phone);
        $stmt->bindParam(":position", $this->position);
        $stmt->bindParam(":department", $this->department);
        $stmt->bindParam(":is_primary", $this->is_primary);
        $stmt->bindParam(":notes", $this->notes);
        $stmt->bindParam(":created_at", $this->created_at);
        $stmt->bindParam(":updated_at", $this->updated_at);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function read() {
        $query = "SELECT * FROM " . $this->table_name . " WHERE client_id = ? ORDER BY is_primary DESC, last_name ASC, first_name ASC";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->client_id);
        $stmt->execute();

        return $stmt;
    }

    public function readOne() {
        $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        $this->client_id = $row['client_id'];
        $this->first_name = $row['first_name'];
        $this->last_name = $row['last_name'];
        $this->email = $row['email'];
        $this->phone = $row['phone'];
        $this->position = $row['position'];
        $this->department = $row['department'];
        $this->is_primary = $row['is_primary'];
        $this->notes = $row['notes'];
        $this->created_at = $row['created_at'];
        $this->updated_at = $row['updated_at'];
    }

    public function update() {
        $query = "UPDATE " . $this->table_name . " SET client_id=:client_id, first_name=:first_name, last_name=:last_name, email=:email, phone=:phone, position=:position, department=:department, is_primary=:is_primary, notes=:notes, updated_at=:updated_at WHERE id=:id";

        $stmt = $this->conn->prepare($query);

        $this->client_id = htmlspecialchars(strip_tags($this->client_id));
        $this->first_name = htmlspecialchars(strip_tags($this->first_name));
        $this->last_name = htmlspecialchars(strip_tags($this->last_name));
        $this->email = htmlspecialchars(strip_tags($this->email));
        $this->phone = htmlspecialchars(strip_tags($this->phone));
        $this->position = htmlspecialchars(strip_tags($this->position));
        $this->department = htmlspecialchars(strip_tags($this->department));
        $this->notes = htmlspecialchars(strip_tags($this->notes));
        $this->is_primary = $this->is_primary ? 1 : 0;
        $this->updated_at = date('Y-m-d H:i:s');

        $stmt->bindParam(":client_id", $this->client_id);
        $stmt->bindParam(":first_name", $this->first_name);
        $stmt->bindParam(":last_name", $this->last_name);
        $stmt->bindParam(":email", $this->email);
        $stmt->bindParam(":phone", $this->phone);
        $stmt->bindParam(":position", $this->position);
        $stmt->bindParam(":department", $this->department);
        $stmt->bindParam(":is_primary", $this->is_primary);
        $stmt->bindParam(":notes", $this->notes);
        $stmt->bindParam(":updated_at", $this->updated_at);
        $stmt->bindParam(":id", $this->id);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function getFullName() {
        return trim($this->first_name . ' ' . $this->last_name);
    }

    public function getDisplayName() {
        $name = $this->getFullName();
        if (!empty($this->position)) {
            $name .= ' - ' . $this->position;
        }
        if ($this->is_primary) {
            $name .= ' (Primary)';
        }
        return $name;
    }
}
?>
