Write a function to merge two datasets while handling duplicates.
Implement merge_datasets(first, second, key) for datasets represented as lists of dictionaries. Records with the same value for key are duplicates, and the record from second replaces the matching record from first. Preserve the order of each key's first appearance, and return a new list without modifying either input.
Signature: def merge_datasets(first, second, key):
Each record contains the specified key, and all key values are hashable. If a key appears multiple times within either dataset, the last record for that key supplies its values.
def merge_datasets(first, second, key):